Now, let’s do the next step. The function call (squares-of (1 2 3 4 5 6 ...)) simply returns a list of the squares of the numbers in its argument list. So it returns (1 4 9 16 25 36 49 64 ...). If we replace this function call with its value, our program simply becomes: (take 25 (1 4 9 16 25 36 49 64 ...)) And, of course, the value of that function call is simply: (1 4 9 16 25 36 49 64 ... 576 625) Now let’s look at our program again: (take 25 (squares-of (integers))) Notice that it has no variables. Indeed, it has nothing more than three functions and one constant. Try writing the squares of integers in Java without using a variable. Oh, there’s probably a way to do it, but it certainly isn’t natural, and it wouldn’t read as nicely as my program above. More importantly, if you could peer into the computer’s memory and look at the memory locations used by my program, you’d find that those locations would be initialized as the program first used them, but then they would retain their values, unchanged, throughout the rest of the execution of the program. In other words, no new values would be assigned to those locations. Indeed, this is a necessary condition for Referential Transparency, which depends on the fact that every time you invoke a particular function call, you will get the same result. The fact that computer memory for my program does not change while my program is executing means that the call (f 1) will always return the same value no matter how many times it is called. And that means I can replace (f 1), wherever it appears, with its value. Or to say this another way: Referential Transparency means that no function can have a side effect. And, of course, that means that no variable, once initialized, can ever change its value, since assignment is the quintessential side effect. So why is this important? What’s so great about Referential Transparency? Given that it is possible to write programs without assignment, why is it important? You are almost certainly reading this on the screen of your computer. Or if not, you have a computer nearby. How many cores does it have? I’m typing this article on a MacBook Pro with 4 real cores. (They say it has 8, but I don’t count all that “hyperthreading” nonsense. It has four.) My previous laptop had two cores. And the one before that had just one. The only conclusion I can draw is that my next laptop will truly have eight cores and the one after that will likely have 16. The poor hardware engineers, who have carried us on their backs for the last four decades, have finally hit the speed of light limit. Computer clocks simply aren’t going to get much faster. After doubling every 18 months for longer than most programmers (except me) have lived, the runaway growth in computer speed has slammed to a halt, never to rise again. PragPub January 2013 6
Purchased by unknown, nofirst nolast From: Scampersandbox (scampersandbox.tizrapublisher.com)