這是一個家庭作業問題,我試圖做方案的深度優先搜索功能,這是我到目前爲止已經編寫的代碼:方案深度優先搜索圖形函數
(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