8 Iteration and the Loop Macro

Example 8-1. Defining a transposition offset and a twelve tone row.

(define my-key 60) 

(define my-row '(0 3 7 11 2 5 9 1 4 6 8 10))

Interaction 8-1. Loop implementation of the transposition program.

cm> (loop for pc in my-row collect (+ pc my-key))
(60 63 67 71 62 65 69 61 64 66 68 70)
cm>

Example 8-2. Defining the fundamental and bounds for a harmonic series.

(define fundamental 220)

(define start-harm 1)

(define end-harm 8)

Interaction 8-2. Loop implementation of the harmonic series iteration.

cm> (loop for h from start-harm to end-harm
          collect (* fundamental h))
(220 440 660 880 1100 1320 1540 1760)
cm>

Interaction 8-3. A loop that sums numbers from zero to ten.

cm> (loop for i to 10 sum i)
55
cm>

Interaction 8-4. The repeat clause.

cm> (loop repeat 10 collect (random 100))
(9 3 93 58 23 0 79 48 27 60)
cm> (loop repeat (random 5) sum 10)
30
cm>

Interaction 8-5. The for clause.

cm> (loop repeat 5 for x = (random 100) minimize x)
12
cm> (loop repeat 5 for x = -99 then (random 10) 
          collect x)
(-99 4 8 6 4)
cm>

Interaction 8-6. The for clause.

cm> (loop for i to 10 collect i)
(0 1 2 3 4 5 6 7 8 9 10)
cm> (loop for i from -1 to 2 by .5 collect i)
(-1 -0.5 0.0 0.5 1.0 1.5 2.0)
cm>

Interaction 8-7. The below clause.

cm> (loop for i below 10 by 2 collect i)
(0 2 4 6 8)
cm>

Interaction 8-8. The downto clause.

cm> (loop for i from 0 downto -10 by 2 collect i)
(0 -2 -4 -6 -8 -10)
cm>

Interaction 8-9. The above clause.

cm> (loop for i from 10 above 2 collect i)
(10 9 8 7 6 5 4 3)
cm>

Interaction 8-10. Example list iterations.

cm> (define test '(a b c))

cm> (loop for v in (reverse test) collect v)
(c b a)
cm> (loop for v in test collect (list v v))
((a a) (b b) (c c))
cm> (loop for v on test collect v)
((a b c) (b c) (c))
cm>

Interaction 8-11. Example action clauses.

cm> (loop repeat 2 do (print 'hi))
hi
hi
#f
cm> (loop repeat 10 collect (random 50))
(43 42 27 18 26 20 44 43 23 30)
cm> (loop repeat 10 sum (random 50))
176
cm> (loop for i below 10 count (even? i))
5
cm> (loop repeat 10 minimize (random 50))
2
cm> (loop repeat 10 maximize (random 50))
43
cm> (loop for x below 10 thereis (= x -1))
#f
cm>

Interaction 8-12. Example conditional clauses.

cm> (loop for i below 10 when (even? i) collect i)
(0 2 4 6 8)
cm> (loop for i below 10 unless (even? i) collect i)
(1 3 5 7 9)
cm> (loop for n = (random 10) until (even? n) collect n)
(9 3)
cm> (loop for n = (random 10) while (even? n) collect n)
()
cm> (loop for i below 10
          for j = (random 10)
          when (even? j) collect i and collect j)
(2 2 4 6 7 4 8 0 9 4)
cm>

Interaction 8-13. Example initialization clause.

cm> (loop with hz = 220
          for h from 1 to 8
          collect (* hz h))
(220 440 660 880 1100 1320 1540 1760)
cm>

Interaction 8-14. Example finalization clause.

cm> (loop repeat 10
          for sum = 0 then (+ sum (random 100))
          finally (return (/ sum 10.0)))
48.6
cm>

Interaction 8-15. Multiple stepping clauses.

cm> (loop for i below 4
          for j from 100 by -10
          for k = (random 100)
          collect (list i j k))
((0 100 1) (1 90 92) (2 80 81) (3 70 2))
cm> (loop for i below 4 
          collect i
          collect (expt 2 i))
(0 1 1 2 2 4 3 8)
cm>

Interaction 8-16. A loop in a loop.

cm> (loop for i from 0 by 10 to 20
          collect (loop repeat 4 
                        for j from i collect j))
((0 1 2 3) (10 11 12 13) (20 21 22 23))
cm>

Chapter Source Code

The source code to all of the examples and interactions in this chapter can be found in the file loop.cm located in the same directory as the HTML file for this chapter. The source file can be edited in a text editor or evaluated inside the Common Music application.