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)
its printing the final result that blows the stack so it's the REPL not your code.
replace the last line with
(count (stress-test 99999))
and it finishes
the stack trace has this pattern repeating many times:
13: core_print.clj:58 clojure.core/print-sequential
14: core_print.clj:140 clojure.core/fn
15: MultiFn.java:167 clojure.lang.MultiFn.invoke
EDIT: LDomagala pointed out print-level as a safety against this sort of crash
user> (set! *print-level* 20)
20
user> (stress-test 9999)
((:f (:f (:f (:f (:f (:f (:f (:f (:f (:f (:f (:f (:f (:f (:f (:f (:f (:f (:f # \a) \a) \a) \a) \a) \a) \a) \a) \a) \a) \a) \a) \a) \a) \a) \a) \a) \a) \a) "")
user>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With