从方案中的深度递归循环获取变量

发布于 2024-10-28 10:20:29 字数 1015 浏览 1 评论 0原文

我有一个深度递归过程,当它找到问题的解决方案时会输出一个列表,但这个列表是非常深度递归创建的。这是代码:(

(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,而不是所有数字的列表我想。如何让我的代码输出最后一行的那一秒,以便我可以操作它返回的列表?

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) '())
)

(I alologize for the strange formatting)

Anyway, this outputs the following:

...
()
()
(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

It's that second from the last line that I need. As you can see, when I display 'final-path' I get what I want, but for some reason (I think because of all the recursive calls) the actual variable at the end is just 7, not the list of all the numbers I want. How can I get my code to output that second from the last line, so I can manipulate the list it returns?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤独岁月 2024-11-04 10:20:29

噢...这段代码可以更简单。让我提出一些建议。

首先,在父函数内部定义助手对你没有帮助;它使得独立测试变得不可能;将其移到外面。

接下来,我不清楚为什么你有两个参数,“路径”和“最终路径”;一行目的陈述在这里确实很有帮助。其中一部分是决定您的函数在失败时应该返回什么。

最后,您确实需要一些测试用例来显示简单输入应该产生什么结果。

我意识到您完全有可能正在寻找“快速解决方案”;我应该告诉你,在其他事情之上抛出可变变量的解决方案肯定不会在我的课堂上获得好成绩......

提前为我傲慢的语气道歉:)。

Oog... this code can be a bunch simpler. Let me make some suggestions.

First, defining the helper inside of the parent function is not helping you; it makes it impossible to test independently; move it outside.

Next, it's not clear to me why you have two arguments, "path" and "final-path"; a one-line purpose statement would be really helpful here. Part of this is deciding what your function is supposed to return on failure.

Finally, you really really need some test cases that show what the thing is supposed to produce for simple inputs.

I realize that it's entirely possible that you're looking for a "quick fix"; I should tell you that a solution that throws a mutable variable on top of the other stuff going on would certainly not a get a good grade in my class...

Apologies in advance for my supercilious tone :).

浅听莫相离 2024-11-04 10:20:29

不要将带有换行符的 final-path 显示为您无法访问的打印输出,而是创建一个列表作为 dfs 对象的成员。然后追加到该列表成员上,并在 dfs 返回时处理该列表。例如:

(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.
)

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.
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文