我有这个代码:
(define graph `(A (B (C)) (D (E)) (C (E))))
(define (prog1 graph)
(let ([seen `()])
(define (sub g)
(cond
[(member (car g) seen) `()]
[else
(set! seen (cons (car g) seen))
(cond
[(null? (cdr g)) (list (car g))]
[else
(cons (car g) (map sub (cdr g)))])]))
(sub graph)))
它打印一个连接图,其中所有节点都出现一次。但是,如果一个节点已经被访问过,我会返回空列表`()
。这会导致输出出现问题,我不知道如何解决:
运行时(prog1 graph)
当前输出为:(A (B (C)) (D (E)) ())
但是,我希望输出是(A (B (C)) (D (E)))
关于如何修改代码以实现这一点的任何提示都会很棒。