Mark,

That did the trick, thanks! I'll definitely be testing my loops for
this sort of thing in the future. It's a little worrisome that it's
this easy to write a major memory leak in Clojure, but I guess this is
just one of the consequences of writing a functional language on top
of the JVM. Maybe it's time for all of us to switch to the CLR! (Rich
mentions that they don't have this problem in the other thread).

CG.

On Nov 27, 6:53 pm, Mark Triggs <mark.h.tri...@gmail.com> wrote:
> 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