tags:

views:

341

answers:

5

Hi, Is there a way I can apply '+ to '( 1 2 3)?

edit: what i am trying to say is that the function i get will be a symbol. Is there a way to apply that?

Thanks.

+3  A: 
(apply (eval '+) '(1 2 3))

Should do it.

Ben Hughes
I got an error " compile: unbound identifier (and no #%top syntax transformer is bound) in: +"
kunjaan
what version of scheme? I tested that in Dr. Scheme before posting
Ben Hughes
This is interesting. the statement by itself will not compile. It needs to be within a function or can be evaluated in the REPL.Are there restrictions to the binding regarding eval?
kunjaan
+1  A: 

How about 'apply'? Use the variable + instead of the symbol + .

(apply + '(1 2 3))

R5RS

Rainer Joswig
A: 

How about the scheme "apply"

(apply + `(1 2 3)) => 6

I hope that was what you were asking :)

micmoo
Hi the thing is + is in terms of a symbol '+ and not a procedure. Apply does not work.
kunjaan
AHHHH, I'm sorry, I didn't see the ' before your +!!!My Bad
micmoo
+2  A: 

In R5RS you need

(apply (eval '+ (scheme-report-environment 5)) '(1 2 3))

The "Pretty Big" language in Dr. Scheme allows for:

(apply (eval '+) '(1 2 3))
Davorak
A: 

;; This works the same as funcall in Common Lisp:
(define (funcall fun . args)
  (apply fun args))

(funcall + 1 2 3 4) => 10
(funcall (lambda (a b) (+ a b) 2 3) => 5
(funcall newline) => *prints newline*
(apply newline) => *ERROR*
(apply newline '()) => *prints newline*

Btw, what's the deal with this "syntax highlighting" ??

Jyaan