Code: Select all
(defun add-n (n)
#'(lambda (x)
(+ x n)))
Code: Select all
((add-n 1) 2)
Code: Select all
? (functionp 'add-n)
NIL
Code: Select all
(defun add-n (n)
#'(lambda (x)
(+ x n)))
Code: Select all
((add-n 1) 2)
Code: Select all
? (functionp 'add-n)
NIL
(first, it's not a lambda function. It is either anonymous function or a lambda expression)anta40 wrote:Here's a function that takes a number N, and returns a function that adds N to its argument (taken from Practical Common Lisp):My guess was something like this should work:Code: Select all
(defun add-n (n) #'(lambda (x) (+ x n)))
Unfortunately, it doesn't work. Further investigation:Code: Select all
((add-n 1) 2)
Now I'm confused. How can I use this add-n properly?Code: Select all
? (functionp 'add-n) NIL
Code: Select all
(funcall (add-n 1) 2)
Code: Select all
(funcall #'+(add-n 1) 2)
Add-n is a function. (add-n 1) is an expression that when evaluated returns the anonymous function.anta40 wrote:Yeah that works. Thanks for you explanation.
BTW, I did tried this, and also failed:Probably because add-n is just an anonymous function?Code: Select all
(funcall #'+(add-n 1) 2)