-1

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
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • Have you looked at the documentation for [`if`](http://www.lispworks.com/documentation/lw50/CLHS/Body/s_if.htm)? `if` takes two or three arguments (the third defaults to `nil`, if not provided), and the second is always the "then"-part, and the third is always the "else"-part. It's not "choosing"; this is just the syntax for the special operator. – Joshua Taylor May 22 '14 at 11:58
  • 2
    This question appears to be off-topic because it is about a basic conditional operator in the language that is clearly answered in the documentation. Questions clearly answered in the documentation don't belong on StackOverflow (see http://meta.stackexchange.com/questions/208372/what-should-one-do-if-they-have-a-question-that-the-answer-is-already-on-google). – Joshua Taylor May 22 '14 at 12:00
  • I'm guess here but I think this is what is confusing you. The unique value NIL is used to represent false in common lisp. It also represents the empty list. Which is to say that NIL is eq to (). All other values are treated as true. In addition NIL evaluates to it's self. So (if '() 'yes 'no) is equivalent to both (if () 'yes 'no) and (if NIL 'yes 'no). – Ben Hyde May 22 '14 at 13:51
  • thanks for your response ben. – user3619045 May 30 '14 at 11:25
  • Hi Joshua, the documentation did show outcome of a certain function it however didn't explain if 'condition' statement, anon put it beautifully in a single line. If I was a intermediate programmer and there were examples floating all over net I wouldn't have bothered .. but this is not the case.. there is a serious lack of lisp support on the net.. I learn what i can from a document but confusions do crop up ..anyhow,thanks for your feedback. – user3619045 May 31 '14 at 03:39

2 Answers2

2

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.

Dair
  • 15,910
  • 9
  • 62
  • 107
0

if special form is used like this:

(if predicate consequent alternative)

In Common Lisp the false value is the empty list nil, represented by either nil or (). The truth value is T and every other value that is not nil. If the predicate evaluates anything except nil it's true and the consequent gets evaluated. If not the alternative is evaluated.

(defun my-not (x)
  (if x nil t))

(my-not ())   ; ==> t
(my-not nil)  ; ==> t
(my-not '())  ; ==> t
(my-not 'nil) ; ==> t
(my-not t)    ; ==> nil
(my-not 'this-is-atruth-value) ; ==> nil
Sylwester
  • 47,942
  • 4
  • 47
  • 79