Thursday, April 19, 2012

How is it possible that loop-recur throws a StackOverflowError?

I´ve been trying to implement a tail-recursive version of chainl1, but even with the loop-recur it throws a StackOverflowError. How is that possible and what can i do to change that?



(defn atest [state]
(when-not (and (= "" state) (not (= (first state) \a)))
(list (first state) (. state (substring 1)))))
(defn op [state]
(when-not (and (= "" state) (not (= (first state) \a)))
(list #(list :| %1 %2) (. state (substring 1)))))
(defn chainl1-helper [x p op]
(fn [state]
(loop [x x
state state]
(if-let [xs (op state)]
(when-let [xs2 (p (second xs))]
(recur ((first xs) x (first xs2)) (second xs2)))
(list x state)))))

(defn chainl1 [p op]
(fn [state]
(when-let [[v s] (p state)]
((chainl1-helper v p op) s))))
(def test-parse (chainl1 atest op))
(defn stress-test [n] (test-parse (apply str (take n (interleave (repeat "a") (repeat "+"))))))
(stress-test 99999)




No comments:

Post a Comment