From: Chris Uzdavinis
Subject: function call
Date: 
Message-ID: <76c3fd0d.0307140642.350d44b6@posting.google.com>
Could someone explain the details of why the following observed
behavior is happening?

(setq f (lambda () 123))
(funcall f) ;; OK

(funcall (car '(f))) ;; Error (void function f)
(funcall (eval (car '(f)))) ;; OK


I'm needing to pull a function out of a list and call it, but don't
like having to use "eval".  Is there an easier way?  And could you
please explain why this is necessary?  Thanks.

Chris

From: Barry Margolin
Subject: Re: function call
Date: 
Message-ID: <1RCQa.146$0z4.94@news.level3.com>
In article <····························@posting.google.com>,
Chris Uzdavinis <·····@uzdavinis.com> wrote:
>Could someone explain the details of why the following observed
>behavior is happening?
>
>(setq f (lambda () 123))
>(funcall f) ;; OK
>
>(funcall (car '(f))) ;; Error (void function f)
>(funcall (eval (car '(f)))) ;; OK
>
>I'm needing to pull a function out of a list and call it, but don't

You don't have a function in the list, you have a symbol in the list.  Why
don't you put the function itself in the list, e.g.

(funcall (car (list f)))

>like having to use "eval".  Is there an easier way?  And could you
>please explain why this is necessary?  Thanks.

When you pass a symbol to FUNCALL, it invokes the function in its function
call.  You only assigned to its value cell.  Try:

(defun f () 123)

or

(setf (symbol-function 'f) (lambda () 123))

and then do (funcall (car '(f))).



-- 
Barry Margolin, ··············@level3.com
Level(3), Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
From: Kenny Tilton
Subject: Re: function call
Date: 
Message-ID: <3F12FC29.3020100@nyc.rr.com>
Chris Uzdavinis wrote:
> Could someone explain the details of why the following observed
> behavior is happening?
> 
> (setq f (lambda () 123))
> (funcall f) ;; OK
> 
> (funcall (car '(f))) ;; Error (void function f)
> (funcall (eval (car '(f)))) ;; OK
> 
> 
> I'm needing to pull a function out of a list and call it, but don't
> like having to use "eval".  Is there an easier way?  And could you
> please explain why this is necessary?  Thanks.

'(f) is short for (quote (f)), and quote kinda says "don't evaluate f, I 
want symbols", whereas you want the function (lambda () 123) you 
associated with the variable f.

The problem here I think is that intoductory texts always seem to set up 
exploratory lists with something like '(1 2 3) or '(a b c). In those 
cases quote is fine, but maybe they should change all those intro texts 
to offer: (list 1 2 3) or (list a b c).

ie, try (funcall (car (list f)))

...and try to remember that ' is quote, which does not evaluate its 
argument.


-- 

  kenny tilton
  clinisys, inc
  http://www.tilton-technology.com/
  ---------------------------------------------------------------
"Everything is a cell." -- Alan Kay
From: Pascal Costanza
Subject: Re: function call
Date: 
Message-ID: <beuu7j$14dm$1@f1node01.rhrz.uni-bonn.de>
Chris Uzdavinis wrote:
> Could someone explain the details of why the following observed
> behavior is happening?
> 
> (setq f (lambda () 123))
> (funcall f) ;; OK
> 
> (funcall (car '(f))) ;; Error (void function f)

This is equivalent to (funcall 'f)

If you (defun f () 123) beforehand then it won't give you an error.

> (funcall (eval (car '(f)))) ;; OK

See the HyperSpec doc for eval:

"To obtain the current dynamic value of a symbol, use of symbol-value is 
equivalent (and usually preferable) to use of eval."

So you can also say (funcall (symbol-value (car '(f))))

> I'm needing to pull a function out of a list and call it, but don't
> like having to use "eval".  Is there an easier way?  And could you
> please explain why this is necessary?

Because it is written! ;)

 From the HyperSpec doc for funcall:

"If function is a symbol, it is coerced to a function as if by finding 
its functional value in the global environment."

That's exactly what happens when you do (funcall 'f) - here f is the 
symbol that is passed to funcall.


Note that you cannot get the contents of a local variable by way of 
symbols because the compiler is allowed to compile away the information 
what names were used for locals.

This:

(let ((x 5))
   (symbol-value 'x))

... doesn't work, and the only way to make it work is via special variables.

So for example you could say:

(defvar *x*)

(let ((*x* 5))
   (symbol-value '*x*))

(But please make sure that you understand the concept of special 
variables well beforehand.)


Pascal


-- 
Pascal Costanza               University of Bonn
···············@web.de        Institute of Computer Science III
http://www.pascalcostanza.de  R�merstr. 164, D-53117 Bonn (Germany)