memory of the computer and stare at the memory location that held the value of i, you would see the value held by that memory change from one iteration of the loop to the next. If the previous paragraph seemed to have belabored an obvious point, let me point out that whole papers have been written on this topic. The concepts of identity, value, and state may seem intuitive to us, but they are actually a very rich topic in and of themselves. But I digress. Let’s look at a functional program for the squares of integers. We’ll use the language Clojure for this, though the concepts we’re going to explore work the same in any functional language. (take 25 (squares-of (integers))) Yes, you are reading that correctly and yes, that is an actual program that produces actual results. If you must see the results they are: (1 4 9 16 25 36 49 64 ... 576 625) There are three words in that program: take, squares-of, and integers. Each of those words refers to a function. The left parens in front of those words simply mean: call the following function and treat everything up to the closing right paren as its arguments. The take function takes two arguments, an integer n, and a list l. It returns the first n items of l. The squares-of function takes a list of integers as its argument and returns a list of the squares of those integers. The integers function returns a list of integers in sequential order, starting at 1. That’s it. The program simply takes the first 25 elements of a list of the squares of sequential integers starting at 1. Read that sentence again because I did something important there. I took the three separate definitions of the functions that I gave you in the preceding paragraph and combined them into a single sentence. That’s called: (are you ready for the buzzword?) Referential Transparency. [cue: Fanfare, glitter, ticker tape, cheering crowds] Referential Transparency simply means that, in any given sentence, you can replace the words in that sentence with their definitions, and not change the meaning of the sentence. Or, more importantly for our purposes, it means that you can replace any function call with the value it returns. Let’s see this in action. The function call (integers) returns (1 2 3 4 5 6 ...). OK, I know you’ve got questions about this, right? I mean, how big is this list? The real answer is that the list is only as big as I need it to be but let’s not think about that just now. For the moment just accept that (integers) returns (1 2 3 4 5 6 ...) because it does! Now, in our program, we can replace the function call (integers) with its value. So the program simply becomes: (take 25 (squares-of (1 2 3 4 5 6 ...))) Yes, I did that with copy and paste and that’s also an important point. Referential Transparency is the same as copying the value of a function call and pasting it right on top of that function call. PragPub January 2013 5
Purchased by unknown, nofirst nolast From: Scampersandbox (scampersandbox.tizrapublisher.com)