Chapter 4: Conditionals and Iteration

 

 

If form.

 The syntax of if form is.

             (if test then else)

            First test is evaluated, if not nil then “then” is evaluated, otherwise else is evaluated.

For example.

 

CG-USER(2): (if 1

                (write "hello") (write "hello2"))

 

"hello"

"hello"

 

If is not nil therefore it writes “hello”.

 

CG-USER(3):

CG-USER(3): (if nil (write "hello") (write "hello2”))

 

"hello2"

"hello2"

 

 

Since it is nil therefore it writes “hello2”.

 

An example of if form  is

CG-USER(3): (defun name1 (name2) (if (equal name2 "faiz") (write "hello faiz")(write "hello other"))

              )

 

NAME1

CG-USER(4):

CG-USER(4): (name1 "faiz")

"hello faiz"

"hello faiz"

CG-USER(5): (name1 "arif")

"hello other"

"hello other"

CG-USER(6):

 

This function writes “hello faiz” if the person name is faiz and “hello other” if name if not faiz.

 

The cond macro

     The cond form has the following syntax.

A cond form has a number (possibly zero) of clauses, which are lists of forms. Each clause consists of a test followed by zero or more consequents. For example:

(cond (test-1 consequent-1-1 consequent-1-2 ...) 
      (test-2) 
      (test-3 consequent-3-1 ...) 
      ... )
 

The first clause whose test evaluates to non-nil is selected.

 

For example

CG-USER(8): (Setq a 1)

1

CG-USER(9): (cond ( (eq a 1) (write "hello")) ((eq a 2)( write "hello2")) )

"hello"

"hello"

CG-USER(10):

In the above code, a is set to value 1. If a is 1 then write “hello” and if a is 2 then write “hello2”.

 

A Lisp cond form may be compared to a continued if-then-else as found in many algebraic programming languages:

(cond (p ...)                            if p then ... 
      (q ...)            roughly         else if q then ... 
      (r ...)          corresponds       else if r then ... 
      ...                   to           ... 
      (t ...))                           else ...

 

When macro

 

     The syntax of when macro is

(when test form1 form2 ... )

 

It first evaluates test. If the result is nil, then no form is evaluated, and nil is returned. Otherwise the forms are evaluated sequentially from left to right, and the value of the last one is returned

 

CG-USER(10): (when (eq 0 0 ) (write "hello")(write "world"))

"hello""world"

"world"

CG-USER(11):

 

Since 0 equals 0, therefore “hello” and “world” is written.

 

Loop iteration

 

The loops iteration loops for ever until a value is returned .

 

(defun get-positive-integer ()
  (loop
    (let (enteredval)   ;local variable enteredval initialized to NIL
      (format t "Please enter a positive integer: ")
      (setf enteredval (read))
      (cond ((not (integerp enteredval))
             (format t "~s is not an integer!~%" enteredval))
            ((not (plusp enteredval))
             (format t "~d is not positive!~%" enteredval))
            (t
             (format t "Thank you.  Your value is ~d~%" enteredval)
             (return enteredval))))))
 

 

 

Do iteration

            (do ({(var [init [step]])}*)

   (end-test {result}*)

{ statement}* )

 

Vars can have init values and step increasing values .The end test is the end test of the loop, followed by declaration and statements. For example.

 

CG-USER(3): (do ( (j 0 (+ j 1))) ((equal j 5)) (write j))

 

01234

NIL

CG-USER(4):

 

The algorithm for Shuffle came directly from Knuth, "The Art of Computer Programming." Each time through the loop, a random card (at position k) is removed from the original deck and replaced in its position by the last card in the deck. Meanwhile, the removed card is inserted at the beginning of the shuffled deck that will be returned.

 
(defun shuffle (deck)
    (do ((k (random (length deck)) (random (length deck)))
         (result nil))
        ((null (rest deck)) (cons (first deck) result))
        (setq result (cons (nth k deck) result)
              deck (reverse (cdr (reverse
                                   (subst (car (reverse deck))
                                          (nth k deck)
                                          deck)))))))

 

The result of this function is:

 

CG-USER(10): (setq a (list 1 2 3 4))

(1 2 3 4)

CG-USER(11): (shuffle a)

(3 2 4 1)

CG-USER(12):

 

Exercise:

  1. Write a function that writes “you are welcome” if the user name is Johnson other wise type  user not allowed”.
  2. Write a function using cond macro that reads an input and when it same as the input parameter print yes.
  3. Using do loop write a function that prints the number in reverse order from n to 1 where n is the parameter.