Note that your recur call is wrapped in a cons. It is *not* in the
tail position.
(def date-seq
(fn [d1 d2]
(loop [b (-> d1
(.dayOfMonth)
(.withMinimumValue))
e d2]
(cons b (if (time/before? b e)
(recur (.plusMonths b 1) e)))))) ;; Inside cons, this
call must return
I am not on a machine with clojure at the moment, but offhand you
probably want something shaped more like:
(defn date-seq [d1 d2]
(let [b (-> d1 (.dayOfMonth) (.withMinimumValue))])
(reverse (date-seq-helper b dend (list))))
(defn- date-seq-helper [dbot dend dates]
(let [ndates (cons dbot dates)]
(if (time/before? dbot dend)
(recur (.plusMonths dbot 1) dend ndates) ; Recurse from tail position
ndates))) ; Or end the recursion
Please note that the tail recursion is in a tail position for the
helper call, meaning that there is no computation that must take place
after the call to recur returns.
- dlf
On Mon, May 17, 2010 at 2:36 PM, Base <[email protected]> wrote:
> Hi All -
>
> I am trying convert a function to use loop/recur and am getting the
> dreded
>
> "java.lang.UnsupportedOperationException: Can only recur from tail
> position (repl-1:414)" error
>
> (at least dreded for newbies...)
>
> The function takes 2 joda dates and returns a sequence of dates
> starting at the first of the month until it is past the second date.
> This works using standard recursion but I wanted to use TCO.
>
> Any help is most appreciated (as usual...!)
>
> Thanks
>
> Base
>
>
> (def date-seq
> (fn [d1 d2]
> (loop [b (-> d1
> (.dayOfMonth)
> (.withMinimumValue))
> e d2]
> (cons b (if (time/before? b e)
> (recur (.plusMonths b 1) e))))))
>
> --
> 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
--
--
Dave Fayram
[email protected]
--
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