Messiaen Quartet for the end of time And another.

30
Messiaen Quartet for the end of time And another

Transcript of Messiaen Quartet for the end of time And another.

Page 1: Messiaen Quartet for the end of time And another.

Messiaen

• Quartet for the end of time

• And another

Page 2: Messiaen Quartet for the end of time And another.

Now for the fun (hard) part

• Recursion

• Necessary to work on LISTS of data where we want to affect each element of the list as in

• (add-one ‘(1 2 3 4))

• (2 3 4 5)

Page 3: Messiaen Quartet for the end of time And another.

Recursion

• Lisp is widely known for its use of recursion, functions that call themselves during execution with lesser and lesser elements of their arbitrary-length list arguments.

• Using recursion therefore means that users need not know the actual length of list arguments in order to access them, a great advantage when manipulating vast amounts of information.

Page 4: Messiaen Quartet for the end of time And another.

• The following code describes a user-defined recursive function also called add-one that adds 1 to each of the elements in its list argument.

(defun add-one (list-of-numbers) (if (null list-of-numbers) () (cons (+ 1 (first list-of-numbers)) (add-one (rest list-of-numbers)))))

Page 5: Messiaen Quartet for the end of time And another.

How it works:

• (line 1) the first line of code names the function and declares the variables that will represent its argument(s) in a list (in this case only one named argument exists).

(defun add-one (list-of-numbers)

Page 6: Messiaen Quartet for the end of time And another.

Continue

• (line 2) the function if in this case tests the argument list-of-numbers to ascertain if it contains data, and if empty it then returns an empty list (second line of code)

(if (null list-of-numbers) ()

Page 7: Messiaen Quartet for the end of time And another.

Continue

• (line 3) if (else) the list still contains entries, the code constructs a list consisting of 1 added to the first of add-one's list argument

(cons (+ 1 (first list-of-numbers))

Page 8: Messiaen Quartet for the end of time And another.

Continue

• (line 4) the result of add-one's application to the remainder of the list.

(add-one (rest list-of-numbers)))))

Page 9: Messiaen Quartet for the end of time And another.

Here we go again:

(defun add-one (list-of-numbers) (if (null list-of-numbers) () (cons (+ 1 (first list-of-numbers)) (add-one (rest list-of-numbers)))))

Page 10: Messiaen Quartet for the end of time And another.

Note

• The terminating code (or second line) in the definition of add-one must occur first, because if the function add-one were to attempt to add 1 to an empty list, it would fail (since (), read also as nil, is not a number in its final recursive call).

Page 11: Messiaen Quartet for the end of time And another.

Note

• The terminating code (or second line) in the definition of add-one must occur first, because if the function add-one were to attempt to add 1 to an empty list, it would fail (since (), read also as nil, is not a number in its final recursive call).

Page 12: Messiaen Quartet for the end of time And another.

What really happens

• (cons (+ 1 1 ) (cons (+ 1 2 ) (cons (+ 1 3) ())))• Where the last empty parentheses acts as the empty list in

which all the remainder of the numbers are placed.

Page 13: Messiaen Quartet for the end of time And another.

Again

• Let’s try it all over again

Page 14: Messiaen Quartet for the end of time And another.

Recursion

• The ability of a function to call itself on lesser and lesser amounts of its argument until the argument is empty.

Page 15: Messiaen Quartet for the end of time And another.

Begin

• (defun add-one (list-of-numbers)

• Looks pretty much like before, but remember that the variable “list-of-numbers” is going to be a list that we will want to add one to each of its elements.

Page 16: Messiaen Quartet for the end of time And another.

Hard part

• We know that if we call the function over and over again it will finally find an empty list and if we try to add 1 to nil we’ll get an error.

• Thus the first thing we want to do is to make sure we have a default way to escape the recursion.

Page 17: Messiaen Quartet for the end of time And another.

• (defun add-one (list-of-numbers) (if (null list-of-numbers) nil

What this does is if the list is empty, then we want it to return nil or an empty list. This will become clear when we finish the function.

Page 18: Messiaen Quartet for the end of time And another.

Here’s where “cons comes in handy

• (defun add-one (list-of-numbers) (if (null list-of-numbers) () (cons (+ 1 (first list-of-numbers))

We collect the first of the list altered by adding one to it.

Page 19: Messiaen Quartet for the end of time And another.

And now

• (defun add-one (list-of-numbers) (if (null list-of-numbers) () (cons (+ 1 (first list-of-numbers)) (add-one (rest list-of-numbers)))))

We’re done and time to review the process. Especially the last line where the recursion is.

Page 20: Messiaen Quartet for the end of time And another.

Recursion

• (defun add-one (list-of-numbers) ;;;name the fcn and arg (if (null list-of-numbers) () ;;;ensure no mistake (cons (+ 1 (first list-of-numbers)) ;;;cons add (add-one (rest list-of-numbers))))) ;;;recurse

Note how each time the function recurses, it does so without its first element. Slowly this moves down the list until it’s nil when the conditional “if” saves us from adding a number (1) to a nil (error!!!!).

Page 21: Messiaen Quartet for the end of time And another.

Pretend

• We have (add-one ‘(1 2 3))• Then it works like this:

(cons (+ 1 1 ) (cons (+ 1 2 ) (cons (+ 1 3) ())))

• (defun add-one (list-of-numbers) ;;;name the fcn and arg (if (null list-of-numbers) () ;;;ensure no mistake (cons (+ 1 (first list-of-numbers)) ;;;cons add (add-one (rest list-of-numbers))))) ;;;recurse

To give us(2 3 4)

The empty list at the end of the above is in line 2 of the function definition.

Page 22: Messiaen Quartet for the end of time And another.

Yet another way to look at it:

QuickTime™ and aTIFF (Uncompressed) decompressor

are needed to see this picture.

Imagine a cylinder

You grab a number from the list, add one to it, thenPush it into the cylinder. Continue doing this until youreach an empty list. Then use the empty list and pull theadded-numbers out one by one (backwards) and put theminto the empty list (backwards) and you end up with theriginal list. The cylinder is called a “stack” in computerize.

Page 23: Messiaen Quartet for the end of time And another.

Whew

• Basically we are done. There are a few things that we will need to learn (other primitive names, alternative ways to do things, save midi files, etc.) but this is pretty much it.

• On the other hand, you will have to REALLY get this in your blood. PRACTICE……

Page 24: Messiaen Quartet for the end of time And another.

Assignment

• Write a normalization function. Normalization is bringing numbers within a particular range. For example, if we have a number that occurs in a range of 1-1000 we might want to make it a pitch (midi pitches for us will be 24 to 96). 500 in the first range would therefore be 60 in the second range. Now, what if we had a whole bunch of numbers in the 1-1000 range. We will need a function that will place all of these numbers in our midi range.

Page 25: Messiaen Quartet for the end of time And another.

You will need two functions:

• 1) you will need a function that will normalize a single number; and then

• 2) you’ll need another function that will run the above function through the entire list of numbers. The result will be a new list of normalized and and thus playable midi notes. We will make music with this next Monday. Everyone will make a piece next week!!!!!

Page 26: Messiaen Quartet for the end of time And another.

Remember to

• DIVIDE AND CONQUER

• No great math is needed, just patience and LOGIC.

Page 27: Messiaen Quartet for the end of time And another.

I am at

[email protected], so if you really have problems, send me your code an d/or questions.

• OR do you want to set up the email chat list now?????????

Page 28: Messiaen Quartet for the end of time And another.

• Also, check out the new exercises on the class website.

Page 29: Messiaen Quartet for the end of time And another.

Final projects

• One more week to figure these out?

Page 30: Messiaen Quartet for the end of time And another.

Next week

• We will all create our own works in class and play them for one another!