Function Definitions

8  -  Iteration (Repetition)

repeat ..... Executes one or more expressions a given number of times and returns the value of the last expression

(repeat 4 (prompt "hi "))  displays hi hi hi hi and returns nil
After (setq k 0)
(repeat 4 (setq k (1+ k)))  returns 4
After (setq c 10)
(repeat 3 (princ c) (princ "/") (setq c (+ c 5))) displays 10/15/20/ and returns 25
After (setq L1 (list a b c d e f g))
(repeat 3 (setq L1 (cdr L1))) returns (D E F G)

The first argument must be an integer.


while ..... Repeatedly executes one or more expressions while a test expression remains T (non-nil) and returns the value of the last expression

After (setq c 0 n 100)
(while (< c 5)
   (setq c (1+ c))
   (setq n (- n 2))
   (princ n) (princ " "))

The above expression prints 98 96 94 92 90. The first argument "(< c 5)" is the test expression. The first time into the while loop, the test expression is evaluated.  If it returns T (non-nil), the following expressions are executed.  Then the test expression is evaluated again and, if it passes (returns T), the following expressions are executed again.  This continues until the test expression fails (returns nil), at which point the while loop is exited.


foreach ..... Substitutes a series of values (taken from a list) in a given function or series of functions and returns the value of the last function

(foreach word (list "a " "few " "birds ") (prompt word))  displays "a few birds" and returns nil.  This gives the same result as the  following: (prompt "a ") (prompt "few ") (prompt "birds ")

After (setq h 88 j 55 k 33)
(foreach x (list  'h  'j  'k) (princ x) (princ "  =  ") (princ (eval x)) (terpri))  produces
 H = 88
  J = 55
 K = 33
and returns nil (from the terpri)


HOME


Copyright © 1988, 1998 Ronald W. Leigh