lisp哈希符号替换返回的值

时间:2014-03-29 14:16:18

标签: search graph lisp common-lisp allegro-cl

我一直在尝试为图形构建类似于广度优先树状结构的东西,其中包含来自给定节点的所有可能路径。我对算法没有问题,就像我弹出的某种错误一样。以下是相关代码:

(set 'my-graph '((A (B C))
                 (B (D E))
                 (C (F G))
                 (D (E))
                 (E (H))
                 (F (H I))
                 (G (I))
                 (H (J))
                 (I (J))
                 (J ())))


(defun search-tree(graph traversed visited)
  (cond
   ((null traversed) NIL)
   (:else (let*
              ((new-visited (append visited (list (car traversed))))
               (children (add-children graph (car traversed)
                                       (append (cdr traversed) new-visited))))
            (cond
             ((null children) (list (car traversed)))
             (:else 
              (cons (car traversed)
                    (mapcar (lambda(x) (search-tree graph (list x) new-visited)) children)))
             )
            )
          )
   )
  )

;;; Selects the node to pick returned children from
(defun add-children(graph node visited)
  (cond
   ((null graph) NIL)
   ((equal (caar graph) node) (new-nodes (cadar graph) visited))
   (:else (add-children (cdr graph) node visited))
   )
  )

;;; Returns new, unvisited nodes from the children of a node
(defun new-nodes(children visited)
  (cond
   ((null children) NIL)
   ((member (car children) visited) (new-nodes (cdr children) visited))
   (:else (cons (car children) (new-nodes (cdr children) visited)))
   )
  )

函数搜索树被称为(搜索树my-graph'(A)'())并且它几乎返回所有内容,但是第一个终端节点被替换为#符号(应该是(J))。这可能是什么问题?
那是返回值。
(A (B (D (E (H #))) (E (H (J)))) (C (F (H (J)) (I (J))) (G (I (J))))) 我试过跟踪代码,但是我仍然不明白为什么(J)列表在#rign符号的中间递归中交换。

1 个答案:

答案 0 :(得分:4)

通常我猜它与*print-level*有关。

此变量控制嵌套列表的打印深度。将其设置为级别的数字。更深层次的列表将替换为#字符。

如果将其设置为NIL没有帮助,那么您可能还需要查阅Allegro CL手册 - 我可以远程记住IDE也有自己的设置。