On Mar 17, 1:17 am, "Steven E. Harris" <s...@panix.com> wrote:
> Michał Marczyk <michal.marc...@gmail.com> writes:
> > a State-using programme builds up a stateful computation first, then
> > uses runState (or perhaps execState / evalState) to run it as a whole;
> > only at this final step does the initial state actually get poured
> > into the opening of the monadic pipe, as it were.
>
> That's a good metaphor.
>
> [...]
>
> > You can use it multiple times, each time injecting a different initial
> > value of state.
>
> Ah, but here's where part of my confusion arises. With this treatment of
> such a built-up computation, it's a kind of a zero-argument function, in
> that there's no way to pass in a "basic value" to kick it off. Where
> does the first "basic value" to be fed into the first monadic function
> come from? Presumably it comes from the first monadic value in the
> "pipe". In your description above, it sounds like this first monadic
> value must be committed to when "buld[ing] up a stateful computation
> first".

Hmm... In Clojure, *values* representing the entire composed
"stateful" computation are functions of the state. therefore, you can
treat it as a regular function and just call (stateful-computation
init-state) as many times as you like. so the first basic value is
simply the one you pass to the composite operation. In haskell, State
monad values have their own type, so you can't use them as functions
directly and need the runState operation. An *operation* on state
monad values is one that returns such a function of state, and the
whole point of the monadic structure is to allow composing these
operations. After all, it's clear that you can't just use function
composition to compose them, because the arguments they expect and the
values they return are not compatible.

Further, any function that creates state monad values can of course be
created dynamically. eg:

(defn make-adder-to-state [x] ; a function
   (fn [v]  ; that returns a monadic function
    (fn [s] ; that returns a state monad value.
      [v (+ x s)])))

which can be used like:

(let [computation (m-bind (m-result 4) (make-adder-to-state 5))]
  (computation 10))
 -> [4 15]

Hope this helps

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