(apply case) doesn't work as expected

Notices and updates
Locked
kinghajj
Posts: 30
Joined: Sun Jul 15, 2007 2:37 pm

(apply case) doesn't work as expected

Post by kinghajj »

These two pieces of code should be equivalent, correct? But they are not. (apply case ...) doesn't work.

Code: Select all

(apply case '(1 (1 2)))

(case 1 (1 2))

kinghajj
Posts: 30
Joined: Sun Jul 15, 2007 2:37 pm

Post by kinghajj »

I wrote a simple macro that will work like apply, but works correctly with case.

Code: Select all

(define-macro (myapply func arglist)
  (eval (cons func (eval arglist))))

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Re: (apply case) doesn't work as expected

Post by cormullion »

Code: Select all

(apply case '(1 (1 2)))
case wants at least two arguments:

(case exp-switch (exp-1 body-1) [ (exp-2 body-2) ... ] )

but you're giving it just a single entity, a quoted list. It's got the switch, but nothing to match and evaluate.

When you want to use apply with a function that wants more than one argument, you can use curry, but I don't know whether that's useful here...

I might be wrong, though... :-)

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post by cormullion »

I might be wrong - the manual has this:
apply should only be used on functions and operators that evaluate all of their arguments, not on special forms like setq or case, which evaluate only some of their arguments. Doing so will cause the function to fail.
So perhaps my previous suggestion was wrong... :-(

Locked