Albiel
Albiel

Reputation: 13

LISP: with predicate as parameter

I want a predicate as a parameter of a function.

(DEFUN per (F L)
    (cond ((F L) 'working)
          (T     'anything)))

(per 'numberp 3)

as result it raises an error:

Undefined operator F in form (F L).

Upvotes: 1

Views: 526

Answers (2)

EdgeLord
EdgeLord

Reputation: 1

Late to the party, but here's another example:

(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))))))

This could be used on predicates such as atom or numberp:

(strip-predicate 'numberp '(a 1 b 2 c 3 d))

(a b c d)

or:

(strip-predicate 'atom '(a (a b) b c d)) 

((a b))

Upvotes: 0

sds
sds

Reputation: 60004

As explained in Technical Issues of Separation in Function Cells and Value Cells, Common Lisp is a Lisp-2, i.e., you need funcall:

(defun per (F L)
  (if (funcall F L)
      'working
      'other))
(per #'numberp 3)
==> WORKING
(per #'numberp "3")
==> OTHER

See also apply.

Upvotes: 4

Related Questions