不是 Lisp 中的预期类型?
Not of the expected type in Lisp?
我正在编写一个程序,它接受一个列表,并用 "X." 替换所有元素(无论是原子、缺点还是列表)所以,如果我要评估我的函数:
((A B) (C . D) E))
会return
((X X) (X . X) X))
但是,我在尝试评估我的函数时不断收到 "X is not of the expected type LIST" 错误。不过,我不明白为什么。我在 Windows 下使用 Allegro Common Lisp Express 版本。我究竟做错了什么?
(defun exify (lst)
(exify-aux lst nil))
(defun exify-aux (lst acc)
(if (null lst)
acc
(if (atom (car lst))
(append 'x acc)
(if (consp (car lst))
(append (cons 'x 'x) acc)
(if (listp (car lst))
(append (exify-aux (car lst) nil) acc)
(if (eq (car lst) nil)
(append nil acc))))))
(exify-aux (cdr lst) acc))
(append 'x acc)
这应该如何运作?附加需要列表。 x
是一个符号。
另外,(append nil acc)
的目的是什么?附加空列表没有任何用处。 (append '() '() '() '())
就是 ()
.
你的代码中有很多死代码,最终是一个无限循环。
(defun exify-aux (lst acc)
;; This whole block if, correct, doesn't amount to any result
(if (null lst)
acc
(if (atom (car lst))
(append 'x acc)
(if (consp (car lst))
(append (cons 'x 'x) acc)
(if (listp (car lst))
(append (exify-aux (car lst) nil) acc)
(if (eq (car lst) nil)
(append nil acc))))))
;; This is the tail call and it's called unconditionally
(exify-aux (cdr lst) acc))
你需要和cons
更亲密。
(defun exify (tree)
(cond ((null tree) nil)
((atom tree) 'x)
(t (cons (exify (car tree))
(exify (cdr tree))))))
当你在的时候,你可能想做高阶函数:
(defun exify (tree)
(accumulate-tree tree
(lambda (_) 'x) ; replaces all non nil leafs with x
#'cons ; copies branches
nil)) ; copies empty leafs
accumualte-tree可以这样定义:
(defun accumulate-tree (tree term combiner null-value)
(labels ((rec (tree)
(cond ((null tree) null-value)
((atom tree) (funcall term tree))
(t (funcall combiner (rec (car tree))
(rec (cdr tree)))))))
(rec tree)))
我正在编写一个程序,它接受一个列表,并用 "X." 替换所有元素(无论是原子、缺点还是列表)所以,如果我要评估我的函数:
((A B) (C . D) E))
会return
((X X) (X . X) X))
但是,我在尝试评估我的函数时不断收到 "X is not of the expected type LIST" 错误。不过,我不明白为什么。我在 Windows 下使用 Allegro Common Lisp Express 版本。我究竟做错了什么?
(defun exify (lst)
(exify-aux lst nil))
(defun exify-aux (lst acc)
(if (null lst)
acc
(if (atom (car lst))
(append 'x acc)
(if (consp (car lst))
(append (cons 'x 'x) acc)
(if (listp (car lst))
(append (exify-aux (car lst) nil) acc)
(if (eq (car lst) nil)
(append nil acc))))))
(exify-aux (cdr lst) acc))
(append 'x acc)
这应该如何运作?附加需要列表。 x
是一个符号。
另外,(append nil acc)
的目的是什么?附加空列表没有任何用处。 (append '() '() '() '())
就是 ()
.
你的代码中有很多死代码,最终是一个无限循环。
(defun exify-aux (lst acc)
;; This whole block if, correct, doesn't amount to any result
(if (null lst)
acc
(if (atom (car lst))
(append 'x acc)
(if (consp (car lst))
(append (cons 'x 'x) acc)
(if (listp (car lst))
(append (exify-aux (car lst) nil) acc)
(if (eq (car lst) nil)
(append nil acc))))))
;; This is the tail call and it's called unconditionally
(exify-aux (cdr lst) acc))
你需要和cons
更亲密。
(defun exify (tree)
(cond ((null tree) nil)
((atom tree) 'x)
(t (cons (exify (car tree))
(exify (cdr tree))))))
当你在的时候,你可能想做高阶函数:
(defun exify (tree)
(accumulate-tree tree
(lambda (_) 'x) ; replaces all non nil leafs with x
#'cons ; copies branches
nil)) ; copies empty leafs
accumualte-tree可以这样定义:
(defun accumulate-tree (tree term combiner null-value)
(labels ((rec (tree)
(cond ((null tree) null-value)
((atom tree) (funcall term tree))
(t (funcall combiner (rec (car tree))
(rec (cdr tree)))))))
(rec tree)))