---
created_at: '2013-03-14T15:55:06.000Z'
title: Not Lisp again (2009)
url: http://funcall.blogspot.sg/2009/03/not-lisp-again.html
author: coldtea
points: 226
story_text: ''
comment_text: 
num_comments: 176
story_id: 
story_title: 
story_url: 
parent_id: 
created_at_i: 1363276506
_tags:
- story
- author_coldtea
- story_5375735
objectID: '5375735'

---
[Source](http://funcall.blogspot.sg/2009/03/not-lisp-again.html "Permalink to Abstract Heresies: Not Lisp again....")

# Abstract Heresies: Not Lisp again....

[skip to main ][1] | [skip to sidebar][2]

#  [ Abstract Heresies ][3]

Unorthodox opinions on computer science and programming.

## Thursday, March 5, 2009

###  [Not Lisp again....][4]

It was 1983 and I was taking my first real computer course at MIT. Hal Abelson and Bill Siebert were co-lecturers. Hal was delivering the bad news: 

> “If you already know how to program, you may be at a disadvantage because you will have to unlearn some bad habits.”

Wonderful. I knew how to program (after all, I knew Macro-11, Z80 assembly and BASIC), and I could guess what the bad habits were. Violating the un-cuteness principle was probably one of them. Nothing fun is going to be allowed. (I rarely take notes at lectures. I'm too busy keeping an internal running commentary.) 

> “In this course we will be using the programming language Lisp...”

Argh! Not _that_ again! What is it with Lisp? Ok, maybe at Harvard they do that sort of thing, but this was MIT! Don't they hack computers here?  
  
I was wondering what I had gotten myself in to. I held on to two small hopes. First, that the little amount of Lisp I had learned at Harvard would give me a head start over the other 250 students in the room. Second, that I only had to take a couple of computer classes to fulfill that part of the EECS requirement. I could stomach a bit of Lisp for a while, toe the company line, and be completely un-cute long enough to pass the course.  
  
Hal put the archetypical Lisp program on the blackboard: 
    
    
    (define (fact x)
      (if (zero? x)
          1
          (* x (fact (- x 1)))))
    

He walked the class through the substitution model: 
    
    
    (fact 5)
    (* 5 (fact 4))
    (* 5 (* 4 (fact 3)))
    (* 5 (* 4 (* 3 (fact 2))))
    (* 5 (* 4 (* 3 (* 2 (fact 1)))))
    (* 5 (* 4 (* 3 (* 2 (* 1 (fact 0))))))
    (* 5 (* 4 (* 3 (* 2 (* 1 1)))))
    (* 5 (* 4 (* 3 (* 2 1))))
    (* 5 (* 4 (* 3 2)))
    (* 5 (* 4 6))
    (* 5 24)
    120
    

> “This is a recursive process. At each step in the recursion we break the problem into smaller parts, solve each part separately, then combine the answers. In this example, each recursive call to fact uses a smaller number. Eventually we get to fact 0 which we know is 1\. At this point we can start the multiplication.”

And then he said “This isn't very efficient.”  
  
I thought that was an odd thing to say about a Lisp program. At Harvard, the obvious inefficiency of Lisp was downplayed. Here the professor was pointing it out and was going to do something about it. 

> “Look how the size of the problem depends upon the value of X. When X is five, there are five pending multiplications that have to be kept track of. If X were ten, then there would be ten pending multiplications. The computer is going to have to use up resources to keep track of these. 

> “What we want to do is to keep track of the product of the numbers we've seen so far. Like this: 
    
    
      (define (fact x)
    
        (define (iter n accum)
          (if (zero? n)
              accum
              (iter (- n 1) (* n accum))))
    
        (iter x 1))
    
      (fact 5)
      (iter 5 1)
      (iter 4 5)
      (iter 3 20)
      (iter 2 60)
      (iter 1 120)
      (iter 0 120)
      120
    

> “This is an iterative process. Instead of breaking the problem into parts that we solve separately, we convert the problem into a simpler problem that has the same solution. The solution to (iter 5 1) is the same as the solution to (iter 4 5), which is the same as the solution to (iter 3 20) and so forth. 

> “Notice how in this case we aren't building a chain of pending multiplications. At each iteration, the value of n is being mutiplied by the accumulated product and this becomes the new accumulated product in the next iteration. When n reaches zero, we have in the accumulator the product of all numbers from n down to zero. 

This was pretty obvious to me. However, I thought Hal was omitting an important point. Clearly each recursive call to iter had to be matched by a corresponding (implicit) return. Otherwise, how would the program know how to get back to the caller? Granted, there were no pending multiplications, but that doesn't mean we're not using up stack frames.  
  
But then Hal said, “With an iterative process, the computer doesn't need to use up resources for each iteration. If you know a bit about programming, you might have expected that each iterative call, like each recursive call, would consume a small amount of memory and eventually run out. Our system is smart enough to notice the iteration and avoid doing that.”  
  
Now _that_ caught my attention. I was impressed first by the fact that whoever designed this particular Lisp system cared about efficiency. I was impressed second by the fact that he was able to program the computer to automatically figure it out. I was impressed third that however it was that it worked, it had to be _really_ fast at it (or the cure would be worse than the problem).  
  
Frankly, I had my doubts. I thought that _maybe_ the computer was able to figure this out _some_ of the time, or _maybe_ it was able to substantially reduce, but not totally eliminate, stack allocation. Or maybe this was some sleight of hand: the resources are allocated somewhere else. I tried to figure out how I'd do this in assembly code, but I was baffled. I made a note to myself to check into this.  
  
Hal went on to introduce first-class procedures. I didn't see the rationale for these. I mean, you can't call a function that hasn't been written, and if it has been written then why not just call it directly and avoid the run-around?  
  
Hal said “You're all familiar with the derivative operator.” and he wrote it on the blackboard: 
    
    
                                   f (x + dx) - f (x)
           D f(x) = f'(x) = lim  --------------------
                                          dx
    

> “We can program this as follows: 
    
    
        (define dx .0001)
    
        (define (deriv f)
       
           (define (f-prime x)
             (/ (- (f (+ x dx)) (f x))
                dx))
    
           f-prime)
    

> “Notice how our formula for the derivate translates directly into the lisp code. 

> “Let's take the derivative of the cube function: 
    
    
        (define (cube x) (* x x x))
    
        (define cube-deriv (deriv cube))
    

> “And this new function should act like the derivative of the cube function.”

He put up a slide on the overhead project that showed the above code and the output from the computer: 
    
    
    (cube-deriv 2)
    ;Value: 12.000600010022566
    
    (cube-deriv 3)
    ;Value: 27.00090001006572
    
    (cube-deriv 4)
    ;Value: 48.00120000993502
    

Sure enough, that's reasonably close to 3x^2.  
  
I was floored. Here we take the simple, straightforward definition of the derivative of a function. Type it in with essentially no modification (we're a little cavalier about dropping the limit and just defining dx to be a ‘reasonably small number’) and suddenly the computer knew how to do calculus! This was serious magic. I had never seen anything like it before.  
  
Hal went on to explain how the substitution model of evaluation worked in this example and I carefully watched. It seemed simple. It couldn't be _that_ simple, though, could it? Something must be missing. I had to find out.  
  
As soon as the lecture ended I ran to the Scheme lab. Hewlett Packard had donated several dozen HP 9836 Chipmunks to MIT. Each one was equiped with a blindingly fast 8-MHz 68000 processor. On top of each workstation sat a fairly sizable expansion box that held an additional 16 megabytes of RAM. A student had sole use of an entire machine while working on problem sets. No time-sharing! It was an incredibly lavish setup.   
  
The lab was empty (go figure, everyone had just gotten out of the lecture). I carefully typed in the examples from the lecture and tried them out. I tried the recursive factorial and managed to blow out the stack with a fairly big number, so I knew there was indeed a stack limit and how the machine behaved when you hit it. I removed the multiply from the iterative factorial to avoid making huge numbers and gave it some ridiculously large argument. The machine churned and churned and eventually returned an answer. I gave it a bigger number. A lot more churning, but no sign of stack overflow. I guess that really worked.  
  
Then I typed in the derivative example from the lecture. Sure enough it worked just like Hal said. I tried passing in a few other functions. They worked, too. I did the homework exercises for fun. They just worked.  
  
This was computing on a whole new level. I thought I knew how to program. I didn't know _anything_ like this. I needed to find out whether there was any more of this magic, and exactly how it worked.  
  
My prior objections to Lisp were suddenly far less relevant. 

* Slower than assembly? Maybe for table lookups, but who cares about something as mundane as that? I want to do magic. If I have to look up something in a table, maybe I'll use assembly code.
* Harder to understand? No way. Look at the derivative example. Five lines of code to express the basis of differential calculus. Five lines of assembly code can barely call a subroutine. (As the proud owner of an HP calculator, I had no problem whatsoever with prefix notation.)
* Less efficient? I really didn't know, but it was clear that someone had put some serious thought into it and found some interesting solutions.
* Less capable? Dude, it's doing calculus!
Well, the initial lecture in computer science was a lot more fun and interesting than I expected, but of course I knew they were trying to ‘sell’ comp. sci. as a career. I was pretty sure that this example was a hook and that the tedium would soon follow, perhaps even in the next lecture. 

Posted by Joe Marshall at [9:21 AM][5] [ ![][6] ][7] [ ![][8] ][9]

| ----- |
|  Reactions:  |   | 

[Newer Post][10] [Older Post][11] [Home][3]

Subscribe to: [Post Comments (Atom)][12]

## Google+ Followers

## Blog Archive

* [ ►  ][13] [ 2016 ][14] (4)
    * [ ►  ][13] [ January ][15] (4)
* [ ►  ][13] [ 2015 ][16] (8)
    * [ ►  ][13] [ September ][17] (6)
    * [ ►  ][13] [ August ][18] (2)
* [ ►  ][13] [ 2014 ][19] (11)
    * [ ►  ][13] [ September ][20] (4)
    * [ ►  ][13] [ August ][21] (7)
* [ ►  ][13] [ 2013 ][22] (24)
    * [ ►  ][13] [ October ][23] (2)
    * [ ►  ][13] [ September ][24] (3)
    * [ ►  ][13] [ August ][25] (2)
    * [ ►  ][13] [ July ][26] (12)
    * [ ►  ][13] [ May ][27] (1)
    * [ ►  ][13] [ April ][28] (2)
    * [ ►  ][13] [ March ][29] (1)
    * [ ►  ][13] [ January ][30] (1)
* [ ►  ][13] [ 2012 ][31] (14)
    * [ ►  ][13] [ August ][32] (1)
    * [ ►  ][13] [ July ][33] (1)
    * [ ►  ][13] [ June ][34] (1)
    * [ ►  ][13] [ April ][35] (3)
    * [ ►  ][13] [ March ][36] (3)
    * [ ►  ][13] [ February ][37] (2)
    * [ ►  ][13] [ January ][38] (3)
* [ ►  ][13] [ 2011 ][39] (51)
    * [ ►  ][13] [ November ][40] (4)
    * [ ►  ][13] [ October ][41] (9)
    * [ ►  ][13] [ September ][42] (4)
    * [ ►  ][13] [ August ][43] (1)
    * [ ►  ][13] [ April ][44] (5)
    * [ ►  ][13] [ March ][45] (9)
    * [ ►  ][13] [ February ][46] (10)
    * [ ►  ][13] [ January ][47] (9)
* [ ►  ][13] [ 2010 ][48] (57)
    * [ ►  ][13] [ December ][49] (4)
    * [ ►  ][13] [ October ][50] (1)
    * [ ►  ][13] [ September ][51] (1)
    * [ ►  ][13] [ August ][52] (8)
    * [ ►  ][13] [ July ][53] (1)
    * [ ►  ][13] [ June ][54] (8)
    * [ ►  ][13] [ May ][55] (8)
    * [ ►  ][13] [ April ][56] (18)
    * [ ►  ][13] [ March ][57] (4)
    * [ ►  ][13] [ February ][58] (3)
    * [ ►  ][13] [ January ][59] (1)
* [ ▼  ][13] [ 2009 ][60] (114)
    * [ ►  ][13] [ December ][61] (5)
    * [ ►  ][13] [ November ][62] (2)
    * [ ►  ][13] [ October ][63] (13)
    * [ ►  ][13] [ September ][64] (10)
    * [ ►  ][13] [ August ][65] (9)
    * [ ►  ][13] [ July ][66] (19)
    * [ ►  ][13] [ June ][67] (6)
    * [ ►  ][13] [ May ][68] (7)
    * [ ►  ][13] [ April ][69] (12)
    * [ ▼  ][13] [ March ][70] (21)
        * [Following John Soule][71]
        * [Lisp Conference][72]
        * [Wise up. Flush Scheme.][73]
        * [Lisp conference - Hal Abelson][74]
        * [Thank you][75]
        * [Name Dropping][76]
        * [A little bit of time][77]
        * [Over with][78]
        * [Just about ready][79]
        * [At the conference][80]
        * [On the airplane heading east for ILC 09. I like ha...][81]
        * [Talk to Greenblatt][82]
        * [I like debugging][83]
        * [Fast-forward to 1985][84]
        * [ILC 09 is a go][85]
        * [There's not much more to say about '001\. By the e...][86]
        * [A few speed bumps][87]
        * [Writing blog entries is hard. I'm trying to rememb...][88]
        * [Not Lisp again....][4]
        * [Computers? Ugh.][89]
        * [I hate Lisp][90]
    * [ ►  ][13] [ January ][91] (10)
* [ ►  ][13] [ 2008 ][92] (65)
    * [ ►  ][13] [ December ][93] (1)
    * [ ►  ][13] [ November ][94] (2)
    * [ ►  ][13] [ October ][95] (4)
    * [ ►  ][13] [ September ][96] (7)
    * [ ►  ][13] [ August ][97] (13)
    * [ ►  ][13] [ July ][98] (11)
    * [ ►  ][13] [ May ][99] (5)
    * [ ►  ][13] [ April ][100] (8)
    * [ ►  ][13] [ March ][101] (8)
    * [ ►  ][13] [ February ][102] (5)
    * [ ►  ][13] [ January ][103] (1)
* [ ►  ][13] [ 2007 ][104] (62)
    * [ ►  ][13] [ December ][105] (4)
    * [ ►  ][13] [ November ][106] (4)
    * [ ►  ][13] [ October ][107] (5)
    * [ ►  ][13] [ September ][108] (8)
    * [ ►  ][13] [ August ][109] (5)
    * [ ►  ][13] [ July ][110] (8)
    * [ ►  ][13] [ June ][111] (10)
    * [ ►  ][13] [ May ][112] (5)
    * [ ►  ][13] [ April ][113] (13)
* [ ►  ][13] [ 2006 ][114] (1)
    * [ ►  ][13] [ December ][115] (1)

[ ![][116] ][117]

 

[1]: http://funcall.blogspot.sg#main
[2]: http://funcall.blogspot.sg#sidebar
[3]: http://funcall.blogspot.sg/
[4]: http://funcall.blogspot.sg/2009/03/not-lisp-again.html
[5]: http://funcall.blogspot.sg/2009/03/not-lisp-again.html "permanent link"
[6]: http://www.blogger.com/img/icon18_email.gif
[7]: https://www.blogger.com/email-post.g?blogID=8288194986820249216&postID=3004248047888960327 "Email Post"
[8]: https://resources.blogblog.com/img/icon18_edit_allbkg.gif
[9]: https://www.blogger.com/post-edit.g?blogID=8288194986820249216&postID=3004248047888960327&from=pencil "Edit Post"
[10]: http://funcall.blogspot.sg/2009/03/writing-blog-entries-is-hard.html "Newer Post"
[11]: http://funcall.blogspot.sg/2009/03/computers-ugh.html "Older Post"
[12]: http://funcall.blogspot.com/feeds/3004248047888960327/comments/default
[13]: javascript:void(0)
[14]: http://funcall.blogspot.sg/2016/
[15]: http://funcall.blogspot.sg/2016/01/
[16]: http://funcall.blogspot.sg/2015/
[17]: http://funcall.blogspot.sg/2015/09/
[18]: http://funcall.blogspot.sg/2015/08/
[19]: http://funcall.blogspot.sg/2014/
[20]: http://funcall.blogspot.sg/2014/09/
[21]: http://funcall.blogspot.sg/2014/08/
[22]: http://funcall.blogspot.sg/2013/
[23]: http://funcall.blogspot.sg/2013/10/
[24]: http://funcall.blogspot.sg/2013/09/
[25]: http://funcall.blogspot.sg/2013/08/
[26]: http://funcall.blogspot.sg/2013/07/
[27]: http://funcall.blogspot.sg/2013/05/
[28]: http://funcall.blogspot.sg/2013/04/
[29]: http://funcall.blogspot.sg/2013/03/
[30]: http://funcall.blogspot.sg/2013/01/
[31]: http://funcall.blogspot.sg/2012/
[32]: http://funcall.blogspot.sg/2012/08/
[33]: http://funcall.blogspot.sg/2012/07/
[34]: http://funcall.blogspot.sg/2012/06/
[35]: http://funcall.blogspot.sg/2012/04/
[36]: http://funcall.blogspot.sg/2012/03/
[37]: http://funcall.blogspot.sg/2012/02/
[38]: http://funcall.blogspot.sg/2012/01/
[39]: http://funcall.blogspot.sg/2011/
[40]: http://funcall.blogspot.sg/2011/11/
[41]: http://funcall.blogspot.sg/2011/10/
[42]: http://funcall.blogspot.sg/2011/09/
[43]: http://funcall.blogspot.sg/2011/08/
[44]: http://funcall.blogspot.sg/2011/04/
[45]: http://funcall.blogspot.sg/2011/03/
[46]: http://funcall.blogspot.sg/2011/02/
[47]: http://funcall.blogspot.sg/2011/01/
[48]: http://funcall.blogspot.sg/2010/
[49]: http://funcall.blogspot.sg/2010/12/
[50]: http://funcall.blogspot.sg/2010/10/
[51]: http://funcall.blogspot.sg/2010/09/
[52]: http://funcall.blogspot.sg/2010/08/
[53]: http://funcall.blogspot.sg/2010/07/
[54]: http://funcall.blogspot.sg/2010/06/
[55]: http://funcall.blogspot.sg/2010/05/
[56]: http://funcall.blogspot.sg/2010/04/
[57]: http://funcall.blogspot.sg/2010/03/
[58]: http://funcall.blogspot.sg/2010/02/
[59]: http://funcall.blogspot.sg/2010/01/
[60]: http://funcall.blogspot.sg/2009/
[61]: http://funcall.blogspot.sg/2009/12/
[62]: http://funcall.blogspot.sg/2009/11/
[63]: http://funcall.blogspot.sg/2009/10/
[64]: http://funcall.blogspot.sg/2009/09/
[65]: http://funcall.blogspot.sg/2009/08/
[66]: http://funcall.blogspot.sg/2009/07/
[67]: http://funcall.blogspot.sg/2009/06/
[68]: http://funcall.blogspot.sg/2009/05/
[69]: http://funcall.blogspot.sg/2009/04/
[70]: http://funcall.blogspot.sg/2009/03/
[71]: http://funcall.blogspot.sg/2009/03/following-john-soule.html
[72]: http://funcall.blogspot.sg/2009/03/lisp-conference.html
[73]: http://funcall.blogspot.sg/2009/03/wise-up-flush-scheme.html
[74]: http://funcall.blogspot.sg/2009/03/lisp-conference-hal-abelson.html
[75]: http://funcall.blogspot.sg/2009/03/thank-you.html
[76]: http://funcall.blogspot.sg/2009/03/name-dropping.html
[77]: http://funcall.blogspot.sg/2009/03/little-bit-of-time.html
[78]: http://funcall.blogspot.sg/2009/03/over-with.html
[79]: http://funcall.blogspot.sg/2009/03/just-about-ready.html
[80]: http://funcall.blogspot.sg/2009/03/at-conference.html
[81]: http://funcall.blogspot.sg/2009/03/on-airplane-heading-east-for-ilc-09.html
[82]: http://funcall.blogspot.sg/2009/03/talk-to-greenblatt.html
[83]: http://funcall.blogspot.sg/2009/03/i-like-debugging.html
[84]: http://funcall.blogspot.sg/2009/03/fast-forward-to-1985.html
[85]: http://funcall.blogspot.sg/2009/03/ilc-09-is-go.html
[86]: http://funcall.blogspot.sg/2009/03/theres-not-much-more-to-say-about-001.html
[87]: http://funcall.blogspot.sg/2009/03/few-speed-bumps.html
[88]: http://funcall.blogspot.sg/2009/03/writing-blog-entries-is-hard.html
[89]: http://funcall.blogspot.sg/2009/03/computers-ugh.html
[90]: http://funcall.blogspot.sg/2009/03/i-hate-lisp.html
[91]: http://funcall.blogspot.sg/2009/01/
[92]: http://funcall.blogspot.sg/2008/
[93]: http://funcall.blogspot.sg/2008/12/
[94]: http://funcall.blogspot.sg/2008/11/
[95]: http://funcall.blogspot.sg/2008/10/
[96]: http://funcall.blogspot.sg/2008/09/
[97]: http://funcall.blogspot.sg/2008/08/
[98]: http://funcall.blogspot.sg/2008/07/
[99]: http://funcall.blogspot.sg/2008/05/
[100]: http://funcall.blogspot.sg/2008/04/
[101]: http://funcall.blogspot.sg/2008/03/
[102]: http://funcall.blogspot.sg/2008/02/
[103]: http://funcall.blogspot.sg/2008/01/
[104]: http://funcall.blogspot.sg/2007/
[105]: http://funcall.blogspot.sg/2007/12/
[106]: http://funcall.blogspot.sg/2007/11/
[107]: http://funcall.blogspot.sg/2007/10/
[108]: http://funcall.blogspot.sg/2007/09/
[109]: http://funcall.blogspot.sg/2007/08/
[110]: http://funcall.blogspot.sg/2007/07/
[111]: http://funcall.blogspot.sg/2007/06/
[112]: http://funcall.blogspot.sg/2007/05/
[113]: http://funcall.blogspot.sg/2007/04/
[114]: http://funcall.blogspot.sg/2006/
[115]: http://funcall.blogspot.sg/2006/12/
[116]: https://resources.blogblog.com/img/icon18_wrench_allbkg.png
[117]: //www.blogger.com/rearrange?blogID=8288194986820249216&widgetType=BlogArchive&widgetId=BlogArchive1&action=editWidget&sectionId=sidebar "Edit"

  [*[9:21 AM]: 2009-03-05T09:21:00-08:00

