I've been curious about Lisp since the eighties where my college
Fortran instructor was a Lisp developer. I'm learning Scheme from
"The Little Schemer" book, I've also looked at a view of MIT's
"Structure and Interpretation of Computer Programs" lecture series on
archive.org.
In that series, Dr. Sussman, shows examples of 2 methods to do
addition, both use recursion. One adds 1 to the result of calling
itself multiple times. Dr. Sussman shows how this tends to make the
program bigger. What struck me, was the technique that Dr. Sussman
demonstrates to be less efficient seems to be precisely the technique
being taught in "The Little Schemer" book, although I'm admittedly
only 2/3 of the way through the book. Am I not understanding
something correctly?
-Eric
Sometimes, the most efficient algorithm is not the simpliest one.
And sometimes it doesn't matter.
--
__Pascal Bourguignon__ http://www.informatimago.com/
A bad day in () is better than a good day in {}.
Using recursion to perform addition is simply a training exercise.
In the real world, recursion is used for more sensible tasks.
guile> (define (fac n) (if (< n 2) 1 (* n (fac (- n 1)))))
guile> (fac 9)
362880
guile> (iota 15)
(0 1 2 3 4 5 6 7 8 9 10 11 12 13 14)
guile> (define (pairs x)
(if (< (length x) 2)
'()
(cons (take x 2) (pairs (cddr x)))))
guile> (pairs (iota 15))
((0 1) (2 3) (4 5) (6 7) (8 9) (10 11) (12 13))