1.9
(+ 4 5)
(inc (+ (dec 4) 5))
(inc (+ 3 5))
(inc (inc (+ (dec 3) 5)))
(inc (inc (+ 2 5)))
(inc (inc (inc (+ (dec 2 ) 5))))
(inc (inc (inc (+ 1 5))))
(inc (inc (inc (inc (+ (dec 1) 5)))))
(inc (inc (inc (inc (+ 0 5)))))
(inc (inc (inc (inc 5))))
(inc (inc (inc 6)))
(inc (inc 7))
(inc 8)
9
Looks recursive to me.
(+ 4 5)
(+ (dec 4) (inc 5))
(+ 3 6)
(+ (dec 3) (inc 6))
(+ 2 7)
(+ (dec 2) (inc 7))
(+ 1 8)
(+ (dec 1) (inc 8))
(+ 0 9)
9
Iterative.
1.10 Ackerman function
(A 1 10)
1024
(A 2 4)
65536
(A 3 3)
Machine ran out of memory, segfaulted.
(define (f n) (A 0 n)) => 2n
(define (g n) (A 1 n)) => ?
(define (h n) (A 2 n)) => ?
(define (k n) (* 5 n n) => 5n^2
Note: I researched the Ackerman function on Wikipedia. Interesting function
to say the least. According to SICP, when (= x 0) (* 2 y), but according to
the defn, when (= x 0) (+ 1 y). Am I missing something?
1.11
(define (f-r n)
(cond ((< n 3) n)
(else (+ (f-r (- n 1))
(* 2 (f-r (- n 2)))
(* 3 (f-r (- n 3)))))))
Does Lisp make recusive life easier?
1.12
I looked at rows and columns being equal. Row 1 has a single 1 in it. Row 2
has a 1 in the first and second column. Row three has a one in the first and
third columns, etc. It's the middle numbers that need to be calculated. I
came up with this:
(define (p r c)
(cond ((= r 1) 1)
((= r c) 1)
((= c 1) 1)
(else (+ (p (- r 1) (- c 1))
(p (- r 1) c)))))
1.13
Don't remember enough math to do this one.
I'll hopefully have time over the weekend to continue... If you've done any
of the excercises, please post them.
--
Mark Schoonover, CMDBA
http://www.linkedin.com/in/markschoonover
http://marksitblog.blogspot.com
[EMAIL PROTECTED]
--
[email protected]
http://www.kernel-panic.org/cgi-bin/mailman/listinfo/kplug-lpsg