Conditionals

Please post comments to discussion thread


Here is where we talk about nothing! or nil in lisp terminology.

There are a few functions that respond to nil (and/or T or a value)


The If expression, the cond expression (condition), the not expression, the and expression and or expression.

IF expression

The if expression will return the first option if T (true) or any other value is supplied as the first argument.
The if expression will return the second option if nil is supplied as the first argument.

For example

Code:
(If nil 
 (princ "\nFirst Expression!")
 (princ "\nSecond Expression!")
)
Would print to the command line a carriage return and "Second Expression!"

Progn Expression

The progn expression is typically used in if expressions if you want to do more than on expression

Code:
(If nil 
 (progn
  (princ "\nFirst Expression!")
  (princ "...")
 )
 (progn
  (princ "\nSecond Expression!")
  (princ "...")
 )
)
Would print to the command line a carriage return and "Second Expression!..."

Cond Expression

Code:
(cond (nil (princ "\nNot This One: "))
        (nil (princ "\nNot This One either: "))
        (T (princ "\nYep This One: "))  
)
Now putting nil or T in a conditional doesn't make a lot of sense so most programmers use conditionals with tests.

Tests for numbers can be /=, <=, =, >=

For the examples lets use a very simple variable (setq X 1)

You can also use more than 2 arguments in these expressions!

Examples:

/= Expression

(/= 1 2) would return T

(/= 1 X) would return nil

(/= 1 2 3) would return T

<= Expression

(<= 1 2) would return T

(<= 1 X) would return nil

(<= 1 2 3) would return T

= Expression

(= 1 2) would return nil

(= 1 X) would return T

(= 1 2 3) would return nil

>= Expression

(>= 1 2) would return nil

(>= 1 X) would return T

(>= 3 2 1) would return T

Now you might want to do more than one test in your logic so you would use AND or OR expressions

And Expression

Code:
(and (> X 0)
       (< X 2)
       (= X 1)
)
would return a true

Or Expression

Code:
(or (< X 0)
       (> X 2)
       (/= X 2)
)
I prefer to use And and Or without if expressions (I will show you later how to do that)

Not Expression

(not nil) returns T

(not 1) returns nil

I wanted to do loops first but I think conditionals are essential to exploring loops