Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Applying apply in Scheme

Tags:

apply

scheme

What am I missing here? I was playing with apply in Scheme, and wrote:

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

The way I understand it, the first apply should do:

(apply + '(1 2 3))

and the second should do:

(+ 1 2 3)

But both Ypsilon and Gauche give about the same error (this is Ypsilon's):

error: attempt call non-procedure: (+ 1 2 3)

backtrace:
  0  (apply apply '(+ (1 2 3)))
  ..."/dev/stdin" line 1

What have I failed to understand?

like image 232
JasonFruit Avatar asked Feb 23 '23 17:02

JasonFruit


1 Answers

The problem with '(+ (1 2 3)) is that the + is quoted and thus interpreted as a symbol.

You would have to use eval to get a value for the + symbol.

In other words, what you are trying to do, is not going to work.

Edit: Another option is quasiquote. Eg:

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

Or (without quasiquote)

(apply apply (list + '(1 2 3))); => 6
like image 137
leppie Avatar answered Mar 03 '23 20:03

leppie