LISP:以谓词作为参数
LISP: with predicate as parameter
我想要一个谓词作为函数的参数。
(DEFUN per (F L)
(cond ((F L) 'working)
(T 'anything)))
(per 'numberp 3)
结果引发错误:
Undefined operator F in form (F L).
如 Technical Issues of Separation in Function Cells and Value Cells 中所述,
Common Lisp 是 Lisp-2,也就是说,你
需要 funcall
:
(defun per (F L)
(if (funcall F L)
'working
'other))
(per #'numberp 3)
==> WORKING
(per #'numberp "3")
==> OTHER
另见 apply
。
聚会迟到了,但这是另一个例子:
(defun strip-predicate (p list)
(cond ((endp list) nil)
((funcall p (first list)) (strip-predicate (rest list)))
( T (cons (first list) (strip-Predicate p (rest list))))))
这可以用于谓词,例如 atom 或 numberp:
(strip-predicate 'numberp '(a 1 b 2 c 3 d))
(a b c d)
或:
(strip-predicate 'atom '(a (a b) b c d))
((a b))
我想要一个谓词作为函数的参数。
(DEFUN per (F L)
(cond ((F L) 'working)
(T 'anything)))
(per 'numberp 3)
结果引发错误:
Undefined operator F in form (F L).
如 Technical Issues of Separation in Function Cells and Value Cells 中所述,
Common Lisp 是 Lisp-2,也就是说,你
需要 funcall
:
(defun per (F L)
(if (funcall F L)
'working
'other))
(per #'numberp 3)
==> WORKING
(per #'numberp "3")
==> OTHER
另见 apply
。
聚会迟到了,但这是另一个例子:
(defun strip-predicate (p list)
(cond ((endp list) nil)
((funcall p (first list)) (strip-predicate (rest list)))
( T (cons (first list) (strip-Predicate p (rest list))))))
这可以用于谓词,例如 atom 或 numberp:
(strip-predicate 'numberp '(a 1 b 2 c 3 d))
(a b c d)
或:
(strip-predicate 'atom '(a (a b) b c d))
((a b))