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:
He gives a scheme recursive program to add 2 numbers, any insight would be appreciated!
Scheme version:
(DEFINE (+ X Y)
(IF (= X 0)
Y
(1+ (+ (-1+ X) Y))))
(I've translated to Clojure and added some print statements).(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)))))
here is the output: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
This I do not understand: the code says (= x 0) then y. When x is 0 y is 9 not 13!