Tutorial by Examples

We can repeat an action some number of times using repeat. CL-USER> (loop repeat 10 do (format t "Hello!~%")) Hello! Hello! Hello! Hello! Hello! Hello! Hello! Hello! Hello! Hello! NIL CL-USER> (loop repeat 10 collect (random 50)) (28 46 44 31 5 33 43 35 37 4)
(loop for i in '(one two three four five six) do (print i)) (loop for i in '(one two three four five six) by #'cddr do (print i)) ;prints ONE THREE FIVE (loop for i on '(a b c d e f g) do (print (length i))) ;prints 7 6 5 4 3 2 1 (loop for i on '(a b c d e f g) by #'cddr ...
(defvar *ht* (make-hash-table)) (loop for (sym num) on '(one 1 two 2 three 3 four 4 five 5 six 6 seven 7 eight 8 nine 9 ten 10) by #'cddr do (setf (gethash sym *ht*) num)) (loop for k being each hash-key of *ht* do (print k)) ; iterate over the keys (loop for k ...
Simple LOOP form without special keywords: (loop forms...) To break out of the loop we can use (return <return value>) ` Some examples: (loop (format t "Hello~%")) ; prints "Hello" forever (loop (print (eval (read)))) ; your very own REPL (loop (let ((r (read))) ...
(loop for s being the symbols in 'cl do (print s)) (loop for s being the present-symbols in :cl do (print s)) (loop for s being the external-symbols in (find-package "COMMON LISP") do (print s)) (loop for s being each external-symbols of "COMMON LISP" ...
(loop for i from 0 to 10 do (print i)) ; prints 0 1 2 3 4 5 6 7 8 9 10 (loop for i from 0 below 10 do (print i)) ; prints 0 1 2 3 4 5 6 7 8 9 10 (loop for i from 10 above 0 do (print i)) ; prints 10 9 8 7 6 5 4 3 2 1 (loop for i from 10 to 0 do (print i)) ; prints noth...
We can destructure lists of compound objects CL-USER> (loop for (a . b) in '((1 . 2) (3 . 4) (5 . 6)) collect a) (1 3 5) CL-USER> (loop for (a . b) in '((1 . 2) (3 . 4) (5 . 6)) collect b) (2 4 6) CL-USER> (loop for (a b c) in '((1 2 3) (4 5 6) (7 8 9) (10 11 12)) collect b) (2 5 8 11...
Unlike the loops in nearly every other programming language in use today, the LOOP in Common Lisp can be used as an expression: (let ((doubled (loop for x from 1 to 10 collect (* 2 x)))) doubled) ;; ==> (2 4 6 8 10 12 14 16 18 20) (loop for x from 1 to 10 sum x) ...
LOOP has its own IF statement that can control how the clauses are executed: (loop repeat 1000 for x = (random 100) if (evenp x) collect x into evens else collect x into odds finally (return (values evens odds))) Combining multiple clauses in an IF ...
Multiple FOR clauses are allowed in a LOOP. The loop finishes when the first of these clauses finishes: (loop for a in '(1 2 3 4 5) for b in '(a b c) collect (list a b)) ;; Evaluates to: ((1 a) (2 b) (3 c)) Other clauses that determine if the loop should continue can be combined: ...
The special LOOP NAMED foo syntax allows you to create a loop that you can exit early from. The exit is performed using return-from, and can be used from within nested loops. The following uses a nested loop to look for a complex number in a 2D array: (loop named top for x from 0 below (arr...
Within a LOOP, you can use the Common Lisp (return) form in any expression, which will cause the LOOP form to immediately evaluate to the value given to return. LOOP also has a return clause which works almost identically, the only difference being that you don't surround it with parentheses. The c...
Some examples for a window of size 3: ;; Naïve attempt: (loop for (first second third) on '(1 2 3 4 5) do (print (* first second third))) ;; prints 6 24 60 then Errors on (* 4 5 NIL) ;; We will try again and put our attempt into a function (defun loop-3-window1 (function list) (loop...

Page 1 of 1