User Tools

Site Tools


ch2_1_if

IF

IF is a basic conditional in LISP and accepts two or three arguments. First argument is the condition and is always evaluated. If it is true - means anything not NIL - then the second argument is evaluated and its result returned from IF form. If the condition returns false (NIL), then the third argument is evaluated and returned. When no third argument is supplied, the IF returns NIL in that case.

(if c a)           ; if C is true, then A is evaled, else NIL is returned
 
(if c a b)         ; if C is true, then A is evaled, else B is evaled 

Both A and B are single expressions or single forms, which means that when we want to evaluate more experessions, we need to wrap them in PROGN.

(if c
  (progn
    ...      ; expressions evaluated if C is true
    ...)
  (progn    
    ...      ; expressions evaluated if C is NIL
    ...))           

See also COND, WHEN, UNLESS, AND, OR

ch2_1_if.txt · Last modified: 2022/02/23 08:02 by admin

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki