列表的lisp Cons单元图
这是我的代码列表的lisp Cons单元图,lisp,common-lisp,Lisp,Common Lisp,这是我的代码 (defun f (lst) (cond ((null lst) nil) ((listp (first lst)) nil (f (car lst))) (t (cons (first lst) (list (f (cdr lst))))))) (f '(a (b) c)) ==> (A (B NIL)) 我的目标是(f'(a(b)c))应该返回(a((b.nil)。
(defun f (lst)
(cond ((null lst) nil)
((listp (first lst))
nil
(f (car lst)))
(t (cons (first lst)
(list (f (cdr lst)))))))
(f '(a (b) c))
==> (A (B NIL))
我的目标是(f'(a(b)c))
应该返回(a((b.nil)。(c.nil))
或者(f'(ab))
应该返回(a.(b.nil))
这是一个过程控制单元
我怎么修理它
我想知道的另一件事是工艺符号
要处理我使用的符号,请尝试使用(格式t“)
并递归打印列表
但进展不太顺利
我应该从哪里开始修改?你说的话
若要返回(a((b.nil)。(c.nil))
当参数为(a(b)c)
时,您无需执行任何操作-这些都是相同的(使用:-)
请看一下手册:
(a . (b . ((c . (d . nil)) . (e . nil))))
(a b (c d) e)
你可能是什么意思
如果您需要构造一个字符串“(a.((b.nil)。(c.nil))”
,则需要执行以下操作:
(defun cons-cell-diagram-string (x)
(if (consp x)
(format nil "(~A . ~A)"
(cons-cell-diagram-string (car x))
(cons-cell-diagram-string (cdr x)))
(princ-to-string x)))
(cons-cell-diagram-string '(a (b) c))
==> "(A . ((B . NIL) . (C . NIL)))"
你可能也有这个意思
任务的另一种可能解释是返回列表,但插入点作为字符串:
(defun cons-cell-diagram-list (x &optional (consing-dot "."))
(if (consp x)
(list (cons-cell-diagram-list (car x) consing-dot)
consing-dot
(cons-cell-diagram-list (cdr x) consing-dot))
x))
(cons-cell-diagram-list '(a (b) c))
==> (A "." ((B "." NIL) "." (C "." NIL)))
(cons-cell-diagram-list '(a (b) c) '|.|)
==> (A |.| ((B |.| NIL) |.| (C |.| NIL)))
(cons-cell-diagram-list '(a (b) c) '#\.)
==> (A #\. ((B #\. NIL) #\. (C #\. NIL)))
附言
请注意,我冒昧地按照公认的Lisp编码标准格式化了您的代码
很明显,在listp
子句中有一个多余的nil
(在单独的一行上)
您可能希望使用Emacs编辑代码。您刚才说的
若要返回(a((b.nil)。(c.nil))
当参数为(a(b)c)
时,您无需执行任何操作-这些都是相同的(使用:-)
请看一下手册:
(a . (b . ((c . (d . nil)) . (e . nil))))
(a b (c d) e)
你可能是什么意思
如果您需要构造一个字符串“(a.((b.nil)。(c.nil))”
,则需要执行以下操作:
(defun cons-cell-diagram-string (x)
(if (consp x)
(format nil "(~A . ~A)"
(cons-cell-diagram-string (car x))
(cons-cell-diagram-string (cdr x)))
(princ-to-string x)))
(cons-cell-diagram-string '(a (b) c))
==> "(A . ((B . NIL) . (C . NIL)))"
你可能也有这个意思
任务的另一种可能解释是返回列表,但插入点作为字符串:
(defun cons-cell-diagram-list (x &optional (consing-dot "."))
(if (consp x)
(list (cons-cell-diagram-list (car x) consing-dot)
consing-dot
(cons-cell-diagram-list (cdr x) consing-dot))
x))
(cons-cell-diagram-list '(a (b) c))
==> (A "." ((B "." NIL) "." (C "." NIL)))
(cons-cell-diagram-list '(a (b) c) '|.|)
==> (A |.| ((B |.| NIL) |.| (C |.| NIL)))
(cons-cell-diagram-list '(a (b) c) '#\.)
==> (A #\. ((B #\. NIL) #\. (C #\. NIL)))
附言
请注意,我冒昧地按照公认的Lisp编码标准格式化了您的代码
很明显,在listp
子句中有一个多余的nil
(在单独的一行上)
您可能需要使用Emacs来编辑代码。
(a.((b.nil)。(c.nil))
与(a(b)c)
相同(只是形象化不同,比如四个季度和一美元是相同的金额)。是否希望它返回字符串”(a.((b.nil)。(c.nil))“
?我不想要字符串。像这样(a.((b.nil)。(c.nil)))让函数返回(a.((b.nil)。(c.nil))
而不使用标准格式(a(b)c)
,这是不可能的。您可以返回字符串表示形式,也可以将字符串表示形式打印到屏幕上(不返回)。非常感谢您的帮助(a.((b.nil)。(c.nil))
与(a(b)c)
(只是可视化方式不同,就像四个25美分和一美元是相同的金额)。是否希望它返回字符串”(a.((b.nil)。(c.nil))“
?我不想要字符串。像这样(a.((b.nil)。(c.nil)))让函数返回(a.((b.nil)。(c.nil))
而不使用标准格式(a(b)c)
,这是不可能的。您可以返回字符串表示形式,也可以将字符串表示形式打印到屏幕上(不返回)。非常感谢您的帮助