Dana Vrajitoru
C311 Programming Languages

Lisp Input-Output, Loops

Input Statements

Output Statements

Loop: dotimes

(dotimes (counter limit result)
   sequence of expressions)

Example

(defun factorial (n)
  (let ((f 1))
    (dotimes (i n f)
      (setq f (* f (+ i 1))))))

(factorial 5) ;-> 120
(factorial 1) ;-> 1
(factorial 0) ;-> 1

Loop: dolist

(dolist (variable list result)
  sequence of expressions)

Example

(defun maxl (L)
  "Returns the maximum element of a list."
  (let ((m nil))
    (dolist (x L m)
      (if (or (not m) (> x m))
          (setq m x)))))

(maxl ())            ;-> nil
(maxl '(3 2 6 54 1)) ;-> 54
(maxl '(1))          ;-> 1