Calling function from a list of quoted lambda expressions -


if have list of lambda expressions this:

cl-user> (defparameter list-of-lambda-exp '(#'(lambda (x) x) #'(lambda (x) (* x x)))) 

then how funcall on element of list?

the following doesn't seem work:

cl-user> (funcall (first list-of-lambda-exp) 2) 

gives error

; evaluation aborted on #<type-error expected-type: (or function symbol) datum: #'(lambda (x) x)>.  

... consistent call functionp on (first list-of-lambda-exp). (the above true if remove #' in front of lambda expressions.)

how change (first list-of-lambda-exp) function? cannot seem figure out how write macro either. think i'm making dumb mistake can't figure way out.

there 2 kinds of things happening here. first, remember quote, can abbreviated ' returns argument unevaluated. then, remember #' shorthand function. is, #'(lambda (x) ...) shorthand (function (lambda (x) ...)), not variant of list (lambda (x) ...). thus:

cl-user> (quote ((function (lambda (x) x)) (function (lambda (x) (* x x))))) ;=> (#'(lambda (x) x) #'(lambda (x) (* x x))) 

thus, can either go multiple levels list actual lambda expression (just list starting lambda), can coerce function can call:

cl-user> (let ((fs '(#'(lambda (x) x) #'(lambda (x) (* x x)))))            (list (funcall (coerce (second (first fs)) 'function) 42)                  (funcall (coerce (second (second fs)) 'function) 8))) ;=> (42 64) 

Comments