Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do I use a function as a variable in lisp?

I'm trying to write a function which checks if every element in the list x has property a, so I wrote:

(defun check (a x)
  (if (listp x)
    (eval (cons 'and (mapcar #'a x)))))

but it doesn't work. (Basically I want a to be the name of a function, say blablabla, and in the body of the check-function, by #'a I want to mean the function blablabla, instead of a function called a.) Now the code above doesn't work. I think in Lisp one should be able to plug in functions. How can I fix it?

(It is literally my first day on lisp, so it might be a stupid question ;) and BTW I'm using Lispworks 6.0 personal version.)

like image 876
h__ Avatar asked Feb 04 '26 06:02

h__


1 Answers

There is no need to use the sharp-quote syntax here. Its purpose is to use a function name in a variable position, but a is a variable already. Just write a instead of #'a.

like image 145
Matthias Benkard Avatar answered Feb 08 '26 04:02

Matthias Benkard



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!