evaluating a variable as a function call? 
Author Message
 evaluating a variable as a function call?

Hi, I'm having a bit of trouble trying to do something like this

(define (runme x y)
  ((car x) y)
)
(runme '(car) '(1 2))

In theory, it is like
(define (runme x y)
  (car y)
)
(runme '(car) '(1 2))

which would just give me the "1" back.  But it does not seems to be willing to
the evaluation.  Instead it seems like it is as if I ran

(car (1 2)  ; without the quoting char   '(1 2)  which fails with the
the message "ERROR: Wrong type to apply:  car"

tia,
chil



Tue, 04 May 1999 03:00:00 GMT  
 evaluating a variable as a function call?

Quote:

> Hi, I'm having a bit of trouble trying to do something like this

> (define (runme x y)
>   ((car x) y)
> )
> (runme '(car) '(1 2))

Try the following variant:

        (runme (list car) '(1 2))

Even better, check the procedure "apply".
--

http://www.{*filter*}com.net/~tonyk/



Tue, 04 May 1999 03:00:00 GMT  
 evaluating a variable as a function call?

: Hi, I'm having a bit of trouble trying to do something like this

: (define (runme x y)
:   ((car x) y)
: )
: (runme '(car) '(1 2))

        It's not clear to me quite what you're after, so I'll respond to
both of the possibilities.

        First, the (runme '(car) ...) is not working because once you
quote "car" it's just a symbol.  You want the function that this symbol
points to.  So:

Quote:
> (define (runme fn x)
    (fn x))
> (runme car '(a b c))

'a

        (All examples actually tested in scsh0.4.  Inside Emacs.)

        Now, if you actually want a LIST of functions tossed into runme
you need to use the function (list ...), which does evalate its arguments
(ie, doesn't hide your function away).  So:

Quote:
> (define (runme-2 fn-list x)

    ((car fn-list) x))
Quote:
> (runme-2 (list car cdr map) '(a b c d))

'a

        Hope this helps.

--
William S. Annis                               SysAdmin

Department of Neurology             Neurosimulation Lab



Tue, 04 May 1999 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. indirect function calls and variable variables

2. indirect function calls and variable variables

3. VC++ calling fortran function and fortran function calling a c++ function

4. ? evaluate functions when total # and types of functions are dynamic

5. How to find out name of calling function from called function

6. calling function recursively, local variables?

7. Local variables, call-back functions under Windows

8. Function call or variable?

9. Function call or variable?

10. calling a variable from a separate function

11. Call a function using a variable

12. How update variable values by calling secondary functions ?

 

 
Powered by phpBB® Forum Software