Subject: Re: CASE with symbol evaluating to list?
From: rpw3@rpw3.org (Rob Warnock)
Date: Sun, 23 Nov 2008 21:52:01 -0600
Newsgroups: comp.lang.lisp
Message-ID: <h8udnR_yaOt8ubfUnZ2dnUVZ_ojinZ2d@speakeasy.net>
<guenthert@gmail.com> wrote:
+---------------
| Barry Fishman <barry_fish...@acm.org> wrote:
| > As others have said the case will not evaluate its "keys" arguments.
| > Since +mylist+ is constant you can use #. to evaluate it at read time.
| >
| > (case 2
| >   (#.+mylist+ (format t "~&found~%"))
| >   (otherwise �(format t "~&not found~%")))
| 
| Yes #. was what I needed, thanks (and thank you Wade) for pointing
| that out. I knew it must have been possible to force te evaluation of
| the constant at read time ...
+---------------

But note that for that to work as you expect when compiling [rather
than playing around at the REPL], the definition of +MYLIST+ may have
to be inside an (EVAL-WHEN (:COMPILE-TOPLEVEL :LOAD-TOPLEVEL :EXECUTE) ...)
since some CLs don't automatically make DEFCONSTANT values visible
at compile time[1]. And conversely, if you do that then you will
pollute your compile-time environment with that symbol [which may
or may not cause problems].

As others have pointed out, CASE isn't really buying you anything
over COND in this situation. Using COND really would be better...


-Rob

[1] As is permitted by CLHS "Macro DEFCONSTANT":

      If a defconstant form appears as a top level form, the compiler must
      recognize that name names a constant variable. An implementation
      may choose to evaluate the value-form at compile time, load time,
      or both.

    The subtlety here is that the implementation is *permitted* to
    evaluate the value-form *only* at load time, and thus in the
    absence of an applicable EVAL-WHEN the value of the constant
    variable is *NOT* available at compile time. [At least one
    popular implementation does this.]

-----
Rob Warnock			<rpw3@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607