Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I apply a symbol as a function in Scheme?

Tags:

lisp

scheme

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.

like image 644
unj2 Avatar asked Jun 25 '09 17:06

unj2


4 Answers

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

Should do it.

like image 187
Ben Hughes Avatar answered Sep 22 '22 10:09

Ben Hughes


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))
like image 42
Davorak Avatar answered Sep 18 '22 10:09

Davorak


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

(apply + '(1 2 3))

R5RS

like image 42
Rainer Joswig Avatar answered Sep 22 '22 10:09

Rainer Joswig



;; 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" ??

like image 33
Jyaan Avatar answered Sep 21 '22 10:09

Jyaan