1
我想實現這樣的方案很簡單圖形簡單的圖形表示:名單方案
'((node1 '(node2 node3)) (node2 '()) (node3 '()))
名單,但現在我需要在圖形列表存儲在一個變量。 我嘗試使用定義
(define graph '())
,然後使用此過程增加更多的節點列表。
(define (add-node name children) (list graph (list name children)))
它按預期工作:
(add-node 1 '(2 3))
返回:「(()(1(2 3)))
問題是我未能更新與新添加的圖形節點。 嘗試重新定義圖形會導致「已定義的錯誤」,試圖使用let/let!造成「無法修改恆定錯誤」
任何幫助或建議將不勝感激。
編輯: 感謝@奧斯卡·洛佩斯
我想出了我的問題的解決方案。 我不是一個方案大師,但這裏是我的代碼(ATLEAST工程:))
;Define the empty graph
(define graph '())
; Graph mutator. All modify operations use this procedure
(define (modify-graph newGraph)
(set! graph newGraph)
graph)
; Adds a node to the graph (name '(chidren)) (add-node 1 '(2 3))
(define (add-node name children)
(define (add-node-helper name children graph)
(cons (list name children) graph))
(modify-graph (add-node-helper name children graph)))
; Generic procedure which removes elements from list that match certain condition
(define (remove-element elements condition?)
(cond ((empty? elements) elements)
((condition? (car elements)) (remove-element (cdr elements) condition?))
(else (cons (car elements) (remove-element (cdr elements) condition?))))
)
; Removes a node, and all references to it.
(define (remove name)
(define (remove-node name)
(define (condition? element)
(eq? (car element) name))
(remove-element graph condition?))
(define (remove-all-refs name)
(define (remove-child name node)
(define (condition? element)
(eq? name element))
(cons (car node) (list (remove-element (cadr node) condition?))))
(define (remove-all-refs-helper name graph)
(cond ((empty? graph) graph)
(else (cons (remove-child name (car graph)) (remove-all-refs-helper name (cdr graph))))))
(remove-all-refs-helper name graph))
(modify-graph (remove-node name))
(modify-graph (remove-all-refs name))
)
最終的結果是:
(add-node 1 '(2 3))
(add-node 3 '(5 6))
(add-node 2 '(7 8 9 10 11))
> graph ;-> '((2 (7 8 9 10 11)) (3 (5 6)) (1 (2 3)))
也刪除節點刪除給定節點的所有引用。
你好,抱歉,延遲響應。我去了你的「不推薦」,因爲我覺得它對最終用戶來說更舒服。我用我提出的代碼更新了我的答案。再次感謝 :) –