Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lisp: Why and how does '(nil nil) evaluate to true?

Tags:

lisp

clisp

(if '(nil nil)
    'print-true
    'print-false)

(if '(nil)
    'print-true
    'print-false)

In the code above, why does the Lisp interpreter always evaluate these forms to true (print-true). I thought nil represented false in Common Lisp.

I am using GNU CLISP.

like image 839
zer0c00l Avatar asked Feb 14 '11 06:02

zer0c00l


2 Answers

nil is false. Anything else is true. '(nil) is a list with one element, namely nil. '(nil nil) is a list with two elements, namely nil and nil. Neither of these expressions is the same as nil by itself, so if sees it as true.

like image 102
Josh Lee Avatar answered Sep 24 '22 22:09

Josh Lee


nil is equivalent to an empty list.

CL-USER> (if (list ) 'print-true 'print-false) 
; prints PRINT-FALSE 
CL-USER> (if (list nil) 'print-true 'print-false) 
; prints PRINT-TRUE

'(nil) is equiv to (list nil) which is different from an empty list.

like image 23
Gishu Avatar answered Sep 23 '22 22:09

Gishu