我想要謂詞作爲函數的參數。LISP:謂詞作爲參數
(DEFUN per (F L)
(cond ((F L) 'working)
(T 'anything)))
(per 'numberp 3)
因爲導致它提出了一個錯誤:
Undefined operator F in form (F L).
我想要謂詞作爲函數的參數。LISP:謂詞作爲參數
(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))))))
這可能對謂語,如原子或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))