Thursday, August 19, 2010

Clojure and CPS

Here I translated the reverse in cp-style example in clojure.
This is more an exercise than everything else: since in Clojure there is no TCO at all
(unless explicitly through recur calls), the example grows the stack.

I began translating to trampoline style... but that was not very entertaining. :)

(defn cp-reverse [lst k]
  (loop [current-lst lst current-k k]
    (if (empty? current-lst) (current-k '())
        (recur (rest current-lst)
          (fn [v] (cons (first current-lst)
                        (current-k v)))))))

No comments: