我正在尝试定义自己的函数以使我的hw2更容易,但它不起作用。你能看看它并告诉我我错过了什么吗?
(DEFUN testAL(x)
COND ( ( ATOMP(x) ) 'this-is-an-atom )
( ( LISTP(x) ) 'this-is-a-list )
( T 'this-is-neither ) )
我希望这个条件函数接受输入 X 并输出,如果它是一个原子、列表或两者都不是。问题是当我输入 NIL 时,我收到一个错误:尝试获取未绑定变量"COND"的值。
家庭作业 2 由以下问题组成:
以下哪一个是原子,哪些列表,哪个两者都不是?
A. 无
b. (分 10 3)
c. (ab)
d. 64
e. T
f. (没有像家一样的地方)
g. '(+ 3 5 6)
您的括号不在正确的位置。除非用引号引起引号,否则括号是函数应用程序的开头。
它以未绑定的变量cond
开头。它不是特殊形式(cond (predicate consequent) (predicate2 consequent2))
因为它不以左括号开头。
仅从缩进中,我猜你的意思是写:
(DEFUN testAL (x)
(COND ((ATOM x) 'this-is-an-atom)
((LISTP x) 'this-is-a-list)
(T 'this-is-neither)))
(testal 'test) ; ==> THIS-IS-AN-ATOM
(testal '(a b c)) ; ==> THIS-IS-A-LIST
我已经删除了x
周围的额外括号,因为(x)
表示应用函数x
而x
表示变量x
。如果x
处于不同的位置,例如(+ x 3)
则+
是要应用的函数,x
是其操作数之一。
我把atomp
改成了atom
.由于atom
是 50 年代第一个 LISP 定义的最早原语之一,因此它不像大多数其他谓词那样p
编辑:多匹配
你可以有几个cond
(或if
,因为你在每个测试中只有一个测试),并产生像(print "THIS-IS-AN-ATOM")
这样的副作用,因为你的基本情况永远不会触发(CL中没有列表或原子)。这也许是最简单的解决方案。
(DEFUN testAL (x)
(if (ATOM x) (print 'this-is-an-atom))
(if (LISTP x) (print 'this-is-a-list)))
(testal '()) ; ==> THIS-IS-A-LIST (but prints both)
对于更实用的方法,我会使用高阶函数来保持代码可测试并提供一个产生副作用的打印函数。请注意,对于初学者来说,这可能不那么容易阅读:
;; a list of pairs of predicate and their desription
(defparameter *type-predicates-and-description*
'((douglasp . this-is-the-answer-to-everything)
(floatp . this-is-a-floating-pont-number)
(integerp . this-is-an-integer)
(numberp . this-is-a-number)
(null . this-is-null)
(listp . this-is-a-list)
(characterp . this-is-a-character)
(stringp . this-is-a-string)))
;; custom made predicate
(defun douglasp (x)
(and (numberp x) (= x 42)))
;; returns all the types of a particular value
(defun get-type-info (x)
"return a list if types thet describes argument"
(flet ((check-type (acc type-pair)
"Accumulate description when predicate match"
(if (funcall (car type-pair) x)
(cons (cdr type-pair) acc)
acc)))
;; test x for each type predicate-description
(let ((res (reduce #'check-type
*type-predicates-and-description*
:initial-value '())))
;; check of empty list (no types matched)
(if (null res)
(list 'this-is-neither)
res))))
;; test it
(get-type-info '()) ; ==> (THIS-IS-A-LIST THIS-IS-NULL)
(get-type-info 42) ; ==> (THIS-IS-A-NUMBER
; THIS-IS-AN-INTEGER
; THIS-IS-THE-ANSWER-TO-EVERYTHING)
(get-type-info #()) ; ==> (THIS-IS-NEITHER)
;; Make a function to do side effects
(defun print-type-info (x)
(format t "~{~a~^, ~}." (get-type-info x)))
(print-type-info '()) ; ==> NIL
; and prints "THIS-IS-A-LIST, THIS-IS-NULL."