Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to call a referenced lambda?

I’m trying to call a function (lambda) stored within an alist. Below is a small snippet that demonstrates what I’m trying to do:

(defvar *db* '((:add (lambda (a b)
                       (+ a b)))
               (:sub (lambda (a b)
                       (- a b)))))

(defun perform-operation-on-numbers (operation a b)
  "Performs specified operation on the supplied numbers."
  (let ((func (second (find operation
                            *db*
                            :key #'car))))
    ;; TODO: Call `func` on `a` and `b`
    (print func)))

(perform-operation-on-numbers :add 1 2)

No matter what I do, not even funcall is able to let me call the lambda stored against :add. How should I reference the retrieved lambda as a lambda?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

That’s not a function: it’s a list beginning (lambda ...). If you want a function have a function, for instance by

(defvar *db* `((:add ,(lambda (a b)
                        (+ a b)))
               (:sub ,(lambda (a b)
                        (- a b)))))

or, better, don’t wrap the thing in some useless baggage:

(defvar *db* `((:add ,#'+
               (:sub ,#'-))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading