Wednesday, 2 May 2012

If and Condition

If Format

(if (condition) [process if true] [process if false])

Ex:
(defun max2 (a b)
     (if (> a b) a b)
)




Explanation:
In definition of function, we will compare:
     if a is bigger than b, it will produce a
     if a is not bigger than b, it will produce b (this mean that b is bigger than a, so it produces b)


(max2 3 2)
=> 3

(max2 2 3)
=> 3


Condition Format
(cond     ((first condition) (statement))
              ((second condition) (statement))
              ((n condition) (statement))
              (t (statement))                                => default statement
)


Ex:
(defun number (x)
(cond     ((= x 1) 'a)
              ((= x 2) 'b)
              ((= x 3) 'c)
              ((= x 4) 'd)
              ((= x 5) 'e)
)
)

No comments:

Post a Comment