Hi Charles,

I notice I can stop your version from running out of memory with a very
small change.  Changing from:

  ;; the [f & args] form of trampoline
  (trampoline init start-stream)

to:

  ;; the [f] form of trampoline
  (trampoline #(init start-stream))

seems to work as expected.  I would hazard a guess that this might be
the same issue as described in this thread:

  http://groups.google.com/group/clojure/msg/dfb1c1d68ac7e742

Cheers,

Mark


Charles Gordon <charles.gor...@gmail.com> writes:

> The implementation listed there doesn't work, and is before Rich
> introduced "letfn", so I decided to try my own implementation using
> letfn and trampoline:
>
> (defn machine [start-stream]
>   (letfn [(init [stream]
>                 #(cond (empty? stream) true
>                         (= \c (first stream)) (more (rest stream))
>                         :else false))
>           (more [stream]
>                 #(cond (empty? stream) true
>                         (= \a (first stream)) (more (rest stream))
>                         (= \d (first stream)) (more (rest stream))
>                         (= \r (first stream)) (end (rest stream))
>                         :else false))
>           (end [stream]
>                #(cond (empty? stream) true
>                        :else false))]
>     (trampoline init start-stream)))
>
> This works, but if I try to run it on an infinite sequence, it
> eventually runs out of memory:
>
> (machine (iterate (fn [c] (if (= c \c) \a \d)) \c))
>
> Java heap space
>   [Thrown class java.lang.OutOfMemoryError]

-- 
Mark Triggs
<mark.h.tri...@gmail.com>

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to