Search code examples
functional-programminglispcase-statement

Simple Lisp Case statement question - problem comparing to nil


I'm trying to use a case statement to make some code more readable. It seems to work as a series of if statements, but for some reason the case statement always accepts a comparison to nil even if it is not true. Can someone clarify why this behavior occurs?

Example:

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

In the above example, the first instance returns nil, even though 'a clearly is not nil. Trying to do the same thing with if statements behaves as I would expect:

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

I'm assuming there is some behavior about the case statement I do not understand. Any help would be appreciated.

Edit: Just to clarify, I know that 'a won't be evaluated. I just mocked up this example to create a situation in which the target of the case statement was definitely NOT nil.

I'm using xlisp-plus, but I'm going to try a real clisp install and see if it behaves differently.

Edit (one more time): Installed CLISP and it works fine there. Not really worth the trouble to investigate why xlisp is different. Thanks for the sanity check, everyone.


Solution

  • i think that it depends on your LISP version. I have LispWorks on Mac and my result :

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