English 中文(简体)
a. 从深厚的补习选择中获取变量
原标题:Get a variable from a deep recursive loop in scheme
  • 时间:2011-03-30 22:02:07
  •  标签:
  • scheme

I have a deep recursive process that outputs a list when it finds a solution to the problem, but this list is created very deep recursively. This is the code:

(define (dfs start target)
  (define (dfs-helper start target path new-links final-path)
    (display final-path) (newline)
    (if (null? final-path)
       (if (or (null? new-links) (member start path))  ()
               (first-not-null (lambda (x)
                                 (if (= start target) (dfs-helper x target path  () (append path (list start))) (dfs-helper x target (append path (list start)) (get-neighbors x) final-path)))
                           (get-neighbors start))

       )
       final-path 
    )
 )
 (dfs-helper start target  () (get-neighbors start)  ())
)

(一) 杂质配制。

无论如何,这些产出如下:

...
()
()
(1 7 20 15 22 23 39 40 49 41 31 25 17 18 9 19 26 36 27 12 11 10 3 13 14 21 28 37 43 53 44 52 51 42 50 54 57 58 61 62 60 63)
7

从我所需要的最后一行来看,这是第二点。 正如你可以看到的那样,当我展示最后机会时,我会得到我所希望的东西,但出于某种原因(我想,由于所有休养的电话),最后的实际变量只是7个,而不是我所希望的所有数字的清单。 我怎么能把我的守则带给从最后一行第二行的产出,以便我能够操纵清单。

问题回答

Oog. ......这一法典可以比较简单。 让我提出一些建议。

首先,确定父母职能范围内的助手并不帮助你;这使得无法独立测试;将其移走。

其次,我不清楚你为什么有两点论点,即“pa”和“最后pa”;一线目标说明是really。 这里很有帮助。 其中一部分是决定你的职能是因失败而恢复。

最后,您的实际上需要一些测试案例,表明应该为简单投入做些什么。

我认识到,你完全有可能重新寻找一个“快速解决方案”;我应该告诉你,把一个变数压在另一个陷阱之上的解决办法肯定不会给我的班级带来一个良好的等级。

a. 预先为我的高毒性基调进行道歉:

rather than displaying final-path with a newline as a print-out where you can t get to it, create a list as a member of your dfs object. Then append onto that list member, and process that list on the return of dfs. For instance:

(define (dfs start target)
  (define return-list  ()) ;;add a return list here that you will process later
  (define (dfs-helper start target path new-links final-path)
    ;;(display final-path) (newline)
    (set! return-list (cons final-path return-list)) ;;so you are now "building up" the return list
    (if (null? final-path)
       (if (or (null? new-links) (member start path))  ()
               (first-not-null (lambda (x)
                                 (if (= start target) (dfs-helper x target path  () (append path (list start))) (dfs-helper x target (append path (list start)) (get-neighbors x) final-path)))
                           (get-neighbors start))

       )
       final-path 
    )
 )
 (dfs-helper start target  () (get-neighbors start)  ())

 ;;process the return-list value to get the values you re wanting from it ...
 ;;I m guessing there is something specific you re looking for, i.e., you can
 ;;filter out all the empty-list elements, single-value elements, etc.
)




相关问题
Parsing with DCGs in Scheme (without Prolog)?

Lots of Prolog-in-Scheme implementations are out there. E.g. Kanren, Schelog. Apparently in "Paradigms of AI Programming" Norvig implements Prolog-to-Lisp compiler in Lisp in order to use Definite ...

Applying a symbol as a procedure

Suppose I have a simple symbol: > + + Is there any way I can apply that symbol as a procedure: > ((do-something-with +) 1 2) 3 So that + is evaluated to the procedure +?

string-split in DrScheme

How do I do equivalent of python s str.split in DrScheme? SRFI-13 doesn t seem to have it provided.

Scheme, getting the pointer from pointed struct

Assume I have a such struct: (define-struct node (value next)) ;and making 2 nodes, parent pointing to child as next. (define child (make-node 2 null)) (define parent (make-node 1 child)) Under ...

How to solve the following equation using accumulate (Scheme)

I m trying to do the following problem (there is a formula so I print-screened and uploaded it) Formula http://img248.imageshack.us/img248/6558/problemh.jpg (http://img248.imageshack.us/img248/6558/...