3 ms·
http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-11.html#%_sec_1.2 http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-11.html... This section relies on T
by JoelMcCracken 14y ago
http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-11.html#%_sec_1.2 http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-11.html...
This section relies on TCO. I realize that it could be translated, but I do not feel that this could be done without heavy modification. Besides that, basically the section's goal is to explain TCO.
- irahul 14y ago> Quite a bit of SICP would be awkward to translate into a language that doesn't support TCO, such as Clojure. >> Clojure doesn't have implicit tail call but it does have loop-recur and trampoline. SICP can be followed quite easily in Clojure. >>> http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-11.html.. http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-11.html.... This section relies on TCO. I realize that it could be translated, but I do not feel that this could be done without heavy modification. Besides that, basically the section's goal is to explain TCO. This is the scheme implementation: (define (factorial n) (fact-iter 1 1 n)) (define (fact-iter product counter max-count) (if (> counter max-count) product (fact-iter (* counter product) (+ counter 1) max-count))) And this is the analogous(but not idiomatic) clojure: (defn fact-iter [product counter max-count] (if (> counter max-count) product (recur (* counter product) (inc counter) max-count))) (defn factorial [n] (fact-iter 1 1 n)) There are no heavy modifications. > Besides that, basically the section's goal is to explain TCO. def fact_iter(product, counter, max_count) return product if counter > max_count fact_iter (counter * product), (counter + 1), max_count end def fact(n) fact_iter 1, 1, n end Now Ruby doesn't have TCO. But how does it stop you from understanding TCO, and how to implement them if the runtime supports it? I read SICP some time ago, and didn't do all the exercises, but as far as I recall, chapters 1-3 can easily be followed in Clojure, Ruby, JS...
- JoelMcCracken 14y agoAh, I stand corrected. The last time I used Clojure (a few years ago), recur needed to be used with loop. That looks plenty close enough to the Scheme. I disagree that the Ruby example is sufficient, though. Remember, SICP is intended as an introduction to programming. A chapter like this is useful to people who do /not/ understand TCO, recursion, and the stack already. Understanding it and using that knowledge to translate the exercises is different than having a shaky grasp lisp, scheme, and the "interpretation of computer programs"