Page 1 of 1

why does this line doesn't work?

Posted: Wed Jul 27, 2011 1:12 am
by brooks
I'm using GNU Common Lisp(gcl), when I wrote:

( (lambda (f) (f 1 2)) '+ )

gcl complains:The Function F is undefined. why? It's invalid to pass a function to lambda macro ?

Re: why does this line doesn't work?

Posted: Wed Jul 27, 2011 3:06 am
by ramarren
You probably should not be using GCL. I don't believe it is really maintained anymore, and it doesn't fully conform to the ANSI standard. SBCL is one of the more commonly used open source Common Lisp implementations.

Lambda is not a macro in this context, and calling functions in value space (such as those passed as arguments) doesn't work like this in Common Lisp. I suggest reading a book like Practical Common Lisp where this is explained in detail.

Re: why does this line doesn't work?

Posted: Wed Jul 27, 2011 6:53 am
by brooks
thanks.

I am reading Practical Common Lisp and now I understand the correct code should be:

(lambda (f) (apply f '(1 2))) '+ )

Re: why does this line doesn't work?

Posted: Wed Jul 27, 2011 9:39 am
by gugamilare
If would be better if you do

Code: Select all

(lambda (f) (funcall f 1 2))  '+ )

Re: why does this line doesn't work?

Posted: Wed Jul 27, 2011 9:56 am
by nuntius
Your first example was calling f incorrectly. The next two code samples aren't syntactically correct. I believe this is what you want (and very close to what you started with).

Code: Select all

((lambda (f) (funcall f 1 2)) '+)

Re: why does this line doesn't work?

Posted: Wed Jul 27, 2011 7:00 pm
by brooks
Oh yes, It's really what my want. Thanks a lot.