Search code examples
if-statementcommon-lispland-of-lisp

confusion with Lisp "if" and 'cons' command


I am reading Land of Lisp by Conrad Barski and I am a bit confused on the use of the 'if' command (to name a few)..

So, I understand that writing if '() means that the list is empty and condition is false and if '(1) means the list is non empty and condition is true.

The question is how does lisp know which expression to choose and output based on the nature (True/False) of the if(1) condition? for example in the code below the statement if '(1) is true but then how and why does lisp choose expression I-AM-TRUE as an output?

(if '(1)
    'i-am-true
    'i-am-false)
I-AM-TRUE

similarly how and why does it output How-does-this-happen in example below..

(if '(1)
    'how-does-this-happen
    'any-guesses)
HOW-DOES-THIS-HAPPEN

Solution

  • The structure of an if statement is:

    (if condition true-stament false-statement)
    

    In other words, the first statement (true-statement) always happens when condition evals to true, and the second statement (false-statement) happens when the condition evals to false.