Page 1 of 1

(case) evaluation

Posted: Thu Feb 09, 2006 11:12 pm
by statik
Can anyone tell me why the following is true:

Code: Select all

(setq a 1)
(setq b 2)
(setq c 3)
(setq d 1)

(case d
   (a (do-this))
   (b (do-that))
   (c (do-something))
   (true)
)

=> nil
I understand that the comparative values in my case are unevaluated. When switched out for an (eval) the case still doesn't work.

Code: Select all

(setq a 1)
(setq b 2)
(setq c 3)
(setq d 1)

(case d
   ((eval a) (do-this))
   ((eval b) (do-that))
   ((eval c) (do-something))
   (true)
)

=> nil
What don't I understand?

Posted: Fri Feb 10, 2006 12:03 am
by Sammo
As this example shows, the expression in the exp-N position in each (exp-N body-N) pair is completely unevaluated.

> (set 'a 5)
5
> (set 'test '(eval a))
(eval a)
> (eval test)
5
> (case test (5 'zero) ((eval a) 'one) (pete 'two) (true 'magoo))
one

That is, no attempt is made to evaluate the expression (eval a) in ((eval a) 'one).

Posted: Fri Feb 10, 2006 12:22 am
by Lutz
yes, you have to use constants:

Code: Select all

(case d
   (1 (do-this))
   (2 (do-that))
   (3 (do-something))
   (true)
) 
Lutz

Posted: Fri Feb 10, 2006 8:31 am
by statik
Is there a work around, or can we impliment a change? Does it work this way for a reason?

Posted: Fri Feb 10, 2006 12:02 pm
by cormullion
statik wrote:Is there a work around, or can we impliment a change? Does it work this way for a reason?
Dmitry has written "ecase":

Code: Select all

(define-macro (ecase _v)
  (eval (append (list 'case _v)
		(map (fn (_i) (set-nth 0 _i (eval (_i 0))))
		     (args)))))
(set 'a 1)
(set 'b 2)

(ecase b
   ((eval a) (println "a is 1")) 
   ((eval b) (println "b is 2")) 
   (true) 
)
http://en.feautec.pp.ru/SiteNews/funlib_lsp

Posted: Fri Feb 10, 2006 12:05 pm
by Lutz
'case' is a very fast switcher for the reason that is does not evaluate. For many years actually it was working with evaluation, but then got changed to be more compatible with other LISPs.

Lutz

Posted: Sat Feb 11, 2006 1:16 am
by Fanda
Use 'if' if you want to evaluate...

Fanda