Create a lambda function in runtime lisp -
Create a lambda function in runtime lisp -
let's have function "create-lambda" defined this:
(defun create-lambda(x y) (lambda(z) (let ((foo (some-other-function z y))) (if (= x foo) t))))
if phone call function, this:
(create-lambda 2 3)
it return:
#<function :lambda (z) (let ((foo (some-other-function z y))) (if (= x foo) t))>
what want is, homecoming this:
#<function :lambda (z) (let ((foo (some-other-function z 3))) (if (= 2 foo) t))>
is possible?
it doesn't matter end result since x
, y
bound in lexical scope 2
, 3
. saying don't want:
(let ((x 2) (y 3)) (let ((foo (some-other-function z y))) (if (= x foo) t)))
but want:
class="lang-lisp prettyprint-override">(let ((foo (some-other-function z 3))) (if (= 2 foo) t))
but can't tell difference. can you? anyway. can coerce info construction function this:
class="lang-lisp prettyprint-override">(defun create-lambda (x y) (coerce `(lambda(z) (let ((foo (some-other-function z ,y))) (when (= ,x foo) t))) 'function))
or can create create-lambda macro:
class="lang-lisp prettyprint-override">(defmacro create-lambda (x y) `(lambda(z) (let ((foo (some-other-function z ,y))) (when (= ,x foo) t))))
the differences between these easy spot if phone call side effects(create-lambda (print 3) (print 4))
. in first side effects happen right away since functions evaluate arguments while macro replace whole thing x , y , side effect happen @ phone call time.
lambda lisp
Comments
Post a Comment