From: Novoseltsev Maxim
Subject: list of lambda functions: newbie question
Date: 
Message-ID: <4510f$45a3dd9d$5449a0ce$7877@news.hispeed.ch>
Hello

I have some lambda functions
e.g.
(defun add5 () (lambda (x) (+ x 5)))
(defun mult5 () (lambda (x) (* x 5)))

evaluation as far as i understood should be so

(funcall (add5) 3)

Now I try to make list of these functions and get list pf the values

e.g.

(mapcar (lambda (fun) (funcall fun 2)) '(add5 mult5))

(mapcar (lambda (fun) (funcall fun 2)) '((add5) (mult5)))

But it does not work.

What is the problem?

From: ········@tochka.ru
Subject: Re: list of lambda functions: newbie question
Date: 
Message-ID: <1168399666.539563.110180@i39g2000hsf.googlegroups.com>
> (defun add5 () (lambda (x) (+ x 5)))

To use it: you call ADD5 without arguments; it returns a function; you
call the result with one argument - a number; it returns an increased
number.

> Now I try to make list of these functions and get list pf the values

Of which functions? ADD5 or the result of its call?

> (mapcar (lambda (fun) (funcall fun 2)) '(add5 mult5))

Here you've tried to omit the first call. Should be

(mapcar (lambda (fun) (funcall (funcall fun) 2)) '(add5 mult5))

> (mapcar (lambda (fun) (funcall fun 2)) '((add5) (mult5)))

Here the second argument of MAPCAR not a list of "lambdas"; it is a
list of _texts_ of programs, computing "lambdas". Replace '((add5)
(mult5)) with either (mapcar #'eval '((add5) (mult5))), or  (mapcar
#'funcall '(add5 mult5)), or (list (add5) (mult5)), or (load-time-value
(list (add5) (mult5)) t). The third is, probably, the best.
From: Frank Buss
Subject: Re: list of lambda functions: newbie question
Date: 
Message-ID: <5jh0keq6xy37$.14i2so14ekgpv.dlg@40tude.net>
Novoseltsev Maxim wrote:

> I have some lambda functions
> e.g.
> (defun add5 () (lambda (x) (+ x 5)))
> (defun mult5 () (lambda (x) (* x 5)))

"lambda function" sounds strange, because the lambda function is lambda
itself for me, but maybe I'm wrong. Where did you learn this name?

BTW: add5 is pointless, without a closure. You can simply write
(defun add5 () (+ x 5))
without the additional indirection level.

> (mapcar (lambda (fun) (funcall fun 2)) '(add5 mult5))
> 
> (mapcar (lambda (fun) (funcall fun 2)) '((add5) (mult5)))
> 
> But it does not work.
> 
> What is the problem?

Hint: what does ' mean and what do you want to do?

-- 
Frank Buss, ··@frank-buss.de
http://www.frank-buss.de, http://www.it4-systems.de
From: Peder O. Klingenberg
Subject: Re: list of lambda functions: newbie question
Date: 
Message-ID: <ks3b6kuifa.fsf@beto.netfonds.no>
Novoseltsev Maxim <············@swissonline.ch> writes:

> (mapcar (lambda (fun) (funcall fun 2)) '(add5 mult5))

If you want to map over the symbols, you need another funcall in the
lambda.  (one to call the function associated with the symbol passed
to your lambda, and another to call the returned closure with the
argument 2.)

> (mapcar (lambda (fun) (funcall fun 2)) '((add5) (mult5)))

In this case, the problem is your use of the quote operator.  Nothing
in the quoted list gets evaluated, and I'm guessing you wanted to
evaluate both elements of the list, and run mapcar over a list of
function objects.  Try using LIST instead of the quote.

...Peder...
-- 
I wish a new life awaited _me_ in some off-world colony.
From: Pascal Bourguignon
Subject: Re: list of lambda functions: newbie question
Date: 
Message-ID: <878xgc102q.fsf@thalassa.informatimago.com>
Novoseltsev Maxim <············@swissonline.ch> writes:

> Hello
>
> I have some lambda functions
> e.g.
> (defun add5 () (lambda (x) (+ x 5)))
> (defun mult5 () (lambda (x) (* x 5)))
>
> evaluation as far as i understood should be so
>
> (funcall (add5) 3)
>
> Now I try to make list of these functions and get list pf the values
>
> e.g.
>
> (mapcar (lambda (fun) (funcall fun 2)) '(add5 mult5))

This will run:  (funcall 'add5 2)
                (funcall 'mult5 2)
and would return a list containing the two results, if it wasn't an error:
add5 and mult5 are functions that take no argument. 



> (mapcar (lambda (fun) (funcall fun 2)) '((add5) (mult5)))
>
> But it does not work.
>
> What is the problem?

'((add5) (mult5)) means (quote ((add5) (mult5)))
and quote is a special operator that prevents the evaluation of its argument.
So ((add5) (mult5)) is not evaluated (which is as well, since it's not a form!)

if you want to run the mapcar on a list containing the result of
evaluating calling the function add5 and mult5, then you must just say
so:
 
(mapcar (lambda (fun) (funcall fun 2))     (list (add5) (mult5))


Instead of calling the functions yourself, you could use mapcar to call them:

(mapcar (lambda (fun) (funcall fun 2))     (mapcar (function funcall)  '(add5 mult5)))

this works because add5 and mult5 are symbols, and funcall accepts
symbols, as function designators.  A symbol sym designs the global
function (symbol-function 'sym)


When you have such chained mapcar, it may be good to compose the functions instead:

(mapcar (lambda (fun) (funcall (funcall fun) 2))     '(add5 mult5))


-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

This is a signature virus.  Add me to your signature and help me to live.