Bill Clementson's Blog

Bits and pieces (mostly Lisp-related) that I collect from the ether.

September 2003
Sun Mon Tue Wed Thu Fri Sat
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
Aug  Oct

On Lisp gems

Wednesday, September 3, 2003

One of the things that I like about Paul Graham's book On Lisp is that it contains so many gems. For example, on Page 21, Paul says:

"When we define functions with lambda-expressions, we face a restriction which doesn't arise with defun: a function defined in a lambda-expression doesn't have a name and therefore has no way of referring to itself. This means that in Common Lisp we can't use lambda to define a recursive function."
But then, in the notes on Pages 387-388, he clarifies that by saying that "we cannot define a recursive function with a single lambda-expression" and goes on to show how the typical factorial example:
(defun fact (n)
  (if (= n 0) 
      1
      (* n (fact (- n 1)))))

(fact 8)

could be expressed in terms of anonymous lambda-expressions:
(funcall ((lambda (f) #'(lambda (n) (funcall f f n)))
	  #'(lambda (f n)
	      (if (= n 0)
		  1
		  (* n (funcall f f (- n 1))))))
	 8)
Neat book, eh! And the whole book is like that - a real learning tool. That's why it's listed as one of the 2 "essential" books in my Lisp library.

emacs Copyright © 2004 by Bill Clementson