(case) evaluation

Started by statik, February 09, 2006, 03:12:19 PM

Previous topic - Next topic

statik

Can anyone tell me why the following is true:



(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.



(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?
-statik

Sammo

#1
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).

Lutz

#2
yes, you have to use constants:



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


Lutz

statik

#3
Is there a work around, or can we impliment a change? Does it work this way for a reason?
-statik

cormullion

#4
Quote from: "statik"Is there a work around, or can we impliment a change? Does it work this way for a reason?


Dmitry has written "ecase":


(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">//http://en.feautec.pp.ru/SiteNews/funlib_lsp

Lutz

#5
'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

Fanda

#6
Use 'if' if you want to evaluate...



Fanda