help understanding simple recursion
Posted: Fri May 13, 2016 10:49 am
I am long time programmer wanting to learn LISP (Clojure), watching the 1986 MIT class on the "Structure and Interpretation of Computer Programs" on youtube. 23 minutes 56 seconds into the lecture 1B ;; https://www.youtube.com/watch?v=dlbMuv-jix8
He gives a scheme recursive program to add 2 numbers, any insight would be appreciated!
Scheme version:
(I've translated to Clojure and added some print statements).
here is the output:
This I do not understand: the code says (= x 0) then y. When x is 0 y is 9 not 13!
He gives a scheme recursive program to add 2 numbers, any insight would be appreciated!
Scheme version:
Code: Select all
(DEFINE (+ X Y)
(IF (= X 0)
Y
(1+ (+ (-1+ X) Y))))
Code: Select all
(defn z [x y]
(if (= x 0)
(do (println "x is zero x: " x " y: " y)
y)
(do (println "else x: " x " y: " y)
(inc (z (dec x) y)))))
Code: Select all
user> (z 4 9)
else x: 4 y: 9
else x: 3 y: 9
else x: 2 y: 9
else x: 1 y: 9
x is zero x: 0 y: 9
13