I've been working on a problem that requires me to write a small state
machine for processing XML documents. Out of curiousity (I'm new to
functional programming) I hit Google to see how automata are written
in other functional languages. I found Shriram Krishnamurthi's paper:

http://lambda-the-ultimate.org/node/569

It is discussed further, along with a Clojure implementation,  here:

http://list.cs.brown.edu/pipermail/plt-scheme/2007-November/thread.html#21571

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]

I don't understand the underlying model of Clojure's runtime well
enough to understand why this is occurring. I'm not getting a stack
overflow error, so it seems likely that something is holding onto
memory (perhaps the entire stream?). Does anyone have any insight into
this?

Thanks!
Charles Gordon

-- 
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