In Part 3 of this series, I demonstrated how code can be reused, even when the resulting music is superficially quite different. The next section of the piece also reuses code. The first half of it will simply be a repetition of the transformation created in Part 2, moving from the lowest octaves toward middle C with a melody in the right hand and even 16th notes in the left. The code for generating those notes looked like the following:

```
(setf sers (loop repeat 5 collecting (rnd-order '(0 1 2 3 4 5 6 7 8 9 10 11))))
(setf loci (loop repeat 5 collecting
(sort-asc (filter-first (rnd-pick '(3 4)) (rnd-order '(0 1 2 3 4 5 6 7 8 9 10 11))))))
(setf mels (loop for t in '(0 1 2 3 4 5)
for s in sers
for l in loci collecting
(if (evenp time)
(loop for n in l collecting (+ 36 (* t 6) (nth n s)))
(loop for n in l collecting
(if (< (nth n s) 6)
(+ 36 (* (+ t 1) 6) (nth n s))
(+ 36 (* (- t 1) 6) (nth n s)))))))
(setf left-hand-mels (loop for t in '(0 1 2 3 4 5)
for ser in sers collecting
(if (evenp t)
(loop for s in ser collecting (+ 24 (* t 6) s))
(loop for s in ser collecting
(if (< s 6)
(+ 24 (* (+ t 1) 6) s)
(+ 24 (* (- t 1) 6) s))))))
```

A list of 6 randomly permuted series (**sers**) is created as well as a list of 6 sublists (**loci**) containing index values, which are used to choose melodic notes for the right hand. Then, while looping through t values in the list '(0 1 2 3 4 5) as well as **sers** and **loci** sublists, the notes are selected and transposed based on the t value. If I ran this code again, it would return a variation of the original version due to the randomness involved. That could be an interesting compositional event, but I don't want to explore variations here. Instead, I want to create an extension to the original, that is, I want to repeat the original, which brings the music up to the middle register of the piano and then continue it all the way to the highest register. Because t values determine the transposition - an increase of 1 in the t value transposing the pitch values by 6 semitones - the extension can be created by simply using t values of 6 to 11. So, the code for the extension is:

```
(setf sers (loop repeat 5 collecting (rnd-order '(0 1 2 3 4 5 6 7 8 9 10 11))))
(setf loci (loop repeat 5 collecting
(sort-asc (filter-first (rnd-pick '(3 4)) (rnd-order '(0 1 2 3 4 5 6 7 8 9 10 11))))))
(setf mels (loop for t in '(6 7 8 9 10 11)
for s in sers
for l in loci collecting
(if (evenp time)
(loop for n in l collecting (+ 36 (* t 6) (nth n s)))
(loop for n in l collecting
(if (< (nth n s) 6)
(+ 36 (* (+ t 1) 6) (nth n s))
(+ 36 (* (- t 1) 6) (nth n s)))))))
(setf left-hand-mels (loop for t in '(6 7 8 9 10 11)
for ser in sers collecting
(if (evenp t)
(loop for s in ser collecting (+ 24 (* t 6) s))
(loop for s in ser collecting
(if (< s 6)
(+ 24 (* (+ t 1) 6) s)
(+ 24 (* (- t 1) 6) s))))))
```

The only difference between the original and the extension is the list '(6 7 8 9 10 11) shown in red. This example reveals an important similarity between coding and composition. Adjustments in variable values can create anything from subtle variation to radical transformation, all while the core code remains the same. This parallels compositional practice in which material is defined and transformed by keeping some of its dimensions the same while changing others.

Note that there is a practical problem with the results for the right hand. You may recall that I added the octave above in the melody returned by the code. Because the above process brings the right hand to the highest register of the piano, it's not possible to add the octave for the last 2 transpositions. To solve this, I simply have the right hand play the same music as the left for these two bars.

## Comments