Hello All,
I encountered the following behavior in Clojure 1.6 and wanted to check if
it should be considered a bug or not. I would say yes but wanted to double
check on the list first.
Here's a minimal test case that elicited the error:
(defn f
[xs acc]
(if (nil? xs)
acc
(recur (next xs) (+ (first xs) acc))))
(f [1 2 3 4] 0) => 10
Now, if I want to add pre/post conditions, the following happens:
(defn g
[xs acc]
{:pre [(or (nil? xs) (sequential? xs)) (number? acc)]
:post [number?]}
(if (nil? xs)
acc
(recur (next xs) (+ (first xs) acc))))
=> this fails to compile with "CompilerException
java.lang.UnsupportedOperationException: Can only recur from tail position"
In fact, it is only the post-condition that triggers the issue.
My guess would be that the recur statement is being knocked out of tail
position by the mechanism for handling the post-condition assertion. It can
be fixed in the code by adding an explicit loop:
(defn g2 [xs acc]
{:pre [(or (nil? xs) (sequential? xs)) (number? acc)]
:post [number?]}
(loop [xs xs
acc acc]
(if (nil? xs)
acc
(recur (next xs) (+ (first xs) acc)))))
Thanks,
Michael O'Keefe
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.