A simple Lisp Case case question - a problem compared to nil

I am trying to use the case statement to make the code more readable. It seems to work like a series of if statements, but for some reason, the case statement always accepts a comparison with nil, even if that is not true. Can anyone clarify why this is happening?

Example:

> (case 'a            
    (nil nil)         
    (otherwise 'b))   
NIL                   
> (case 'a            
    ('a 'b)           
    (otherwise nil))  
B                       

In the above example, the first instance returns nil, although "is clearly not zero." Trying to do the same with if statements, as you would expect:

> (if (eq 'a nil) nil 'b)    
B                            
> (if (eq 'a 'a) 'b nil)     
B                            

I assume there is some kind of behavior regarding the case statement, which I don't understand. Any help would be appreciated.

Edit: To clarify, I know that “a will not be evaluated. I simply mocked this example to create a situation in which the case target argument was definitely not null.

xlisp-plus, clisp install , -.

( ): CLISP, . , xlisp . , .

+3
5

, LISP. LispWorks Mac :

CL-USER 2 : 1 > (case 'a            
    (nil nil)         
    (otherwise 'b))   
B
+1

CASE , . CLtL , NIL, , NIL , NIL:

> (case 'a
    ((nil) nil)         
    (otherwise 'b))
B
> (case nil
    ((nil) nil)         
    (otherwise 'b))
NIL
+3

Lisp , CASE . EQL.

(case 'a
  (a 'b)    ; EQL a
  (otherwise 'foo))

(case 'a
  ((a b c) 'foo)   ; EQL to one of a, b or c
  (otherwise 'bar))

. :

; don't use this:
(case 'a
  ('a 'foo)    ; <- bad!  , EQL to QUOTE or A
  (otherwise 'bar))

:

; don't use this:
(case 'a
  ((quote a) 'foo)   ; <- bad! ,  EQL to QUOTE or A
  (otherwise 'bar))
+2

SBCL:

CL-USER> (case 'a
           (nil nil)
           (otherwise 'b))
B

, 'a nil.

+1

case , :

(case 'a
  ((a) 'b)
  (otherwise nil))

- , . otherwise ( ) - t.

BTW, 'a, (quote a), , , quote, :

(case 'quote
  ('a 'b)
  (otherwise nil))
0

All Articles