这是一个家庭作业问题,我试图做方案的深度优先搜索功能,这是我到目前为止已经编写的代码:方案深度优先搜索图形函数
(define explore
(λ(node visited)
(let* ([neighbors (force (cdr node))]
[next (nextNode visited neighbors)]
[is-visited (member? node visited)])
(cond
;if I have no unvisited neighbours print current node and go up one level
[(equal? next #f)
(begin
(display (car node))
(display " "))]
;if current node is not visited and I have unvisited neighbors
;print current node,mark as visited and visit it's neighbors
[(and (equal? is-visited #f) (not (equal? next #f)))
(begin
(display (car node))
(display " ")
(explore next (cons node visited)))])
;go and visit the next neighbor
(if (not (equal? (nextNode (cons next visited) neighbors) #f))
(explore (nextNode (cons next visited) neighbors) (cons node visited))))))
“节点”是当前节点
“访问”是女巫列表我跟踪我访问了
“nextNode”节点是返回第一个未访问过的邻居,如果任何或#F否则
“成员函数? “测试的,如果一个节点是在访问列表
的图形表示中利用相邻使用的节点引用与letrec所以这就是为什么我在“邻居”使用发力: 如:
(letrec([节点1(名单“ NY“(delay(list node2 node3)))],其中node2和node3被定义为node1
我正在处理的问题是我的访问列表丢失了我访问过的一些节点的轨迹在递归中,我该如何解决这个问题?
看来这篇文章只是一个扩展:http://stackoverflow.com/questions/2773878/scheme-accumulative-recursion-with-lists。你应该考虑关闭一个。 – Davorak 2010-05-05 19:13:13