Function (operator) as function parameter: how?

Discussion of Common Lisp
Post Reply
mugmug
Posts: 2
Joined: Wed Dec 26, 2012 5:38 am

Function (operator) as function parameter: how?

Post by mugmug » Wed Dec 26, 2012 5:59 am

Hello,

This newbie is stuck on a problem and hopes any one can give him a hint.
Having used Scheme somewhere in history, I am trying to use Lisp now.

I am stuck on how to do the following Scheme statement in Lisp:

( (lambda (FN) ( FN '( PB PC )))
(lambda (varx) (cons 'PA varx)))
The result is:
(pa pb pc)
the first function takes a function as parameter, which it invokes with a list as argument.

In Lisp I should, I think, quote the second function, like

( (lambda (FN) ( FN '( PB PC ))) '(lambda (varx) (cons 'PA varx)))

But this does not work. I have tried variations with funcall, function # etc, but nothing seems to help.

What is the way to do this in Common Lisp (emacs+cl or CMUcl)?

Greets
JW

Goheeca
Posts: 271
Joined: Thu May 10, 2012 12:54 pm
Contact:

Re: Function (operator) as function parameter: how?

Post by Goheeca » Wed Dec 26, 2012 1:16 pm

I suppose you mean Common Lisp by Lisp.
Because CL is Lisp-2, it evaluates the function position in other way than argument positions so you can't place lambda in a first position. CL has a function which takes a function and arguments and applies them on that: funcall
The code looks:

Code: Select all

(funcall (lambda (fn) (funcall fn '(pb pc))) (lambda (varx) (cons 'pa varx)))
Lisp-2 dialects has two namespaces for variables -- for values and for functions, therefore if you want to apply the + function you must use a special operator function:

Code: Select all

(funcall (function +) 10 10)
which can be abbreviated to:

Code: Select all

(funcall #'+ 10 10)
otherwise it'd look up in the value namespace ... where can be a function, too.
An example:

Code: Select all

(defun fn (a b) (+ a b))
(defvar fn (lambda (a b) (* a b)))
(funcall fn 2 3) ; result = 6
(funcall #'fn 2 3) ; result = 5
cl-2dsyntax is my attempt to create a Python-like reader. My mirror of CLHS (and the dark themed version). Temporary mirrors of aferomentioned: CLHS and a dark version.

mugmug
Posts: 2
Joined: Wed Dec 26, 2012 5:38 am

Re: Function (operator) as function parameter: how?

Post by mugmug » Thu Dec 27, 2012 2:54 pm

thank you !

I must have tried funcall at the wrong place.
There are Lisp-1 interpreters, I now know. I tried eulisp, but it did not even compile on my system.
Well, I ll be fine with the funcalls like in your example, for now.

cheers

nuntius
Posts: 538
Joined: Sat Aug 09, 2008 10:44 am
Location: Newton, MA

Re: Function (operator) as function parameter: how?

Post by nuntius » Thu Dec 27, 2012 3:34 pm

If you want a lisp-1, try Racket (a descendent of, and some might say heir apparent to Scheme).

marcoxa
Posts: 85
Joined: Thu Aug 14, 2008 6:31 pm

Re: Function (operator) as function parameter: how?

Post by marcoxa » Sat Dec 29, 2012 8:46 am

Please note that if you have a LAMBDA, you do not need FUNCALL

Code: Select all

((lambda (x) (+ x 42)) -42) ==> 0
Marco Antoniotti

Goheeca
Posts: 271
Joined: Thu May 10, 2012 12:54 pm
Contact:

Re: Function (operator) as function parameter: how?

Post by Goheeca » Sat Dec 29, 2012 9:19 am

Yeah, that's true, but it's important to understand in which namespace the lambda is and I wanted to avoid a confusion of OP.
In CL I even get used to approach to lambdas just like to any other functions so I originally wanted to write down something in this sense:

Code: Select all

(funcall #'(lambda (x) (1+ x)) 0)
cl-2dsyntax is my attempt to create a Python-like reader. My mirror of CLHS (and the dark themed version). Temporary mirrors of aferomentioned: CLHS and a dark version.

Post Reply