在这个非常有用的答案中,有人建议我可以替换这段代码:
(defun describe-paths (location edges)
(apply (function append) (mapcar #'describe-path
(cdr (assoc location edges)))))
Run Code Online (Sandbox Code Playgroud)
有了这个:
(defun describe-paths-mapcan (location edges)
(mapcan #'describe-path
(cdr (assoc location edges))))
Run Code Online (Sandbox Code Playgroud)
我当然从概念上理解为什么这应该有效,但事实并非如此; 第二个变体冻结我的REPL,CL提示永远不会返回.我必须重启SLIME.所以我查了一下,我想知道mapcan不使用的事实list,而是nconc原因?因此,这些实际上不是功能相同的代码块?
对于好奇,我传递了这个:
(describe-paths-mapcan 'living-room *edges*)
在哪里*edges*:
(defparameter *edges* '((living-room (garden west door)
(attic upstairs ladder))
(garden (living-room east door))
(attic (living-room downstairs ladder))))
Run Code Online (Sandbox Code Playgroud)
和:
(defun describe-path (edge)
`(there is a ,(caddr edge) going ,(cadr edge) from here.))
Run Code Online (Sandbox Code Playgroud)