Page 1 of 1
On lambda function
Posted: Mon Jul 06, 2009 11:34 pm
by anta40
Here's a function that takes a number N, and returns a function that adds N to its argument (taken from Practical Common Lisp):
Code: Select all
(defun add-n (n)
#'(lambda (x)
(+ x n)))
My guess was something like this should work:
Unfortunately, it doesn't work. Further investigation:
Now I'm confused. How can I use this add-n properly?
Re: On lambda function
Posted: Tue Jul 07, 2009 1:17 am
by dmitry_vk
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):
Code: Select all
(defun add-n (n)
#'(lambda (x)
(+ x n)))
My guess was something like this should work:
Unfortunately, it doesn't work. Further investigation:
Now I'm confused. How can I use this add-n properly?
(first, it's not a lambda function. It is either anonymous function or a lambda expression)
'add-n is a symbol. But #'add-n is a function. (add-n 10) is also a function.
The expression ((add-n 1) 2) works in scheme, in common lisp you should use funcall:
(For details, see information about Lisp-1 vs Lisp-2)
Re: On lambda function
Posted: Tue Jul 07, 2009 4:19 am
by anta40
Yeah that works. Thanks for you explanation.
BTW, I did tried this, and also failed:
Probably because add-n is just an anonymous function?
Re: On lambda function
Posted: Tue Jul 07, 2009 4:38 am
by dmitry_vk
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?
Add-n is a function. (add-n 1) is an expression that when evaluated returns the anonymous function.
#' is a "function designator". You should use it only in two cases: #'x where x is a symbol or #'(lambda (args) body).
Re: On lambda function
Posted: Tue Jul 07, 2009 5:04 am
by anta40
Ah OK. I think I understand it.
Re: On lambda function
Posted: Tue Jul 07, 2009 10:49 am
by nuntius
You won't fully understand until you read the following snippet of the spec.
http://www.lispworks.com/documentation/ ... 02_dhb.htm