state-monad

Why must we use state monad instead of passing state directly?

回眸只為那壹抹淺笑 提交于 2019-12-03 03:38:13
Can someone show a simple example where state monad can be better than passing state directly? bar1 (Foo x) = Foo (x + 1) vs bar2 :: State Foo Foo bar2 = do modify (\(Foo x) -> Foo (x + 1)) get State passing is often tedious, error-prone, and hinders refactoring. For example, try labeling a binary tree or rose tree in postorder: data RoseTree a = Node a [RoseTree a] deriving (Show) postLabel :: RoseTree a -> RoseTree Int postLabel = fst . go 0 where go i (Node _ ts) = (Node i' ts', i' + 1) where (ts', i') = gots i ts gots i [] = ([], i) gots i (t:ts) = (t':ts', i'') where (t', i') = go i t (ts

How to put mutable Vector into State Monad

无人久伴 提交于 2019-12-03 03:14:16
I wrote small program in haskell to count all ocurences of Int values in Tree using State Monad with Vector: import Data.Vector import Control.Monad.State import Control.Monad.Identity data Tree a = Null | Node (Tree a) a (Tree a) deriving Show main :: IO () main = do print $ runTraverse (Node Null 5 Null) type MyMon a = StateT (Vector Int) Identity a runTraverse :: Tree Int -> ((),Vector Int) runTraverse t = runIdentity (runStateT (traverse t) (Data.Vector.replicate 7 0)) traverse :: Tree Int -> MyMon () traverse Null = return () traverse (Node l v r) = do s <- get put (s // [(v, (s ! v) + 1)

Why does this simple use of the State monad cause a stack overflow?

喜夏-厌秋 提交于 2019-12-02 22:40:45
I was playing around with the State monad, and I don't know what's causing the stack overflow in this simple piece of code. import Control.Monad.State.Lazy tick :: State Int Int tick = do n <- get put $! (n+1) return n million :: Int million = snd $ runState (mapM_ (const tick) [1..1000000]) 0 main = print million Note I would just like to know what's causing the problem in this piece of code, the task itself is not important per se. The problem is that Control.Monad.State.Lazy's (>>=) is so lazy that even the ($!) doesn't help you. Try Control.Monad.State.Strict, that should reach the ($!).

Confusion over the State Monad code on “Learn you a Haskell”

故事扮演 提交于 2019-12-02 19:04:40
I am trying to get a grasp on Haskell using the online book Learn you a Haskell for great Good . I have, to my knowledge, been able to understand Monads so far until I hit the chapter introducing the State Monad . However, the code presented and claimed to be the Monad implementation of the State type (I have not been able to locate it in Hoogle) seems too much for me to handle. To begin with, I do not understand the logic behind it i.e why it should work and how the author considered this technique.( maybe relevant articles or white-papers can be suggested?) At line 4, it is suggested that

Can I use different workflows simultaneously in F#?

风流意气都作罢 提交于 2019-12-02 07:18:17
I need my state to be passed along while being able to chain functions with the maybe workflow. Is there a way for 2 workflows to share the same context? If no, what is the way of doing it? UPDATE: Well, I have a state that represents a segment of available ID's for the entities that I am going to create in the database. So once an ID is acquired the state has to be transformed to a newer state with the next available ID and thrown away so that nobody can use it again. I don't want to mutate the state for the sake of being idiomatic. The State monad looks like a way to go as it hides the

How can I write this simple code using the state monad?

人走茶凉 提交于 2019-12-01 23:43:44
I'm a beginner at Haskell and I've come across a situation where I would like to use the state monad. (Or at least, I think I that's what I'd like to use.) There are a million tutorials for the state monad, but all of them seem to assume that my main goal is to understand it on a deep conceptual level, and consequently they stop just before the part where they say how to actually develop software with it. So I'm looking for help with a simplified practical example. Below is a very simple version of what my current code looks like. As you can see, I'm threading state through my functions, and

stacking StateT in scalaz

心已入冬 提交于 2019-12-01 15:25:31
I'm trying to understand Monad Transformers in Scala by porting some examples from this tutorial by Dan Piponi: http://blog.sigfpe.com/2006/05/grok-haskell-monad-transformers.html I did a couple of easy ones: import Control.Monad.State import Control.Monad.Identity test1 = do a <- get modify (+1) b <- get return (a,b) test2 = do a <- get modify (++"1") b <- get return (a,b) go1 = evalState test1 0 go2 = evalState test2 "0" becomes: import scalaz._, Scalaz._ val test1 = for { a <- get[Int] _ <- modify[Int](1+) b <- get } yield (a,b) val test2 = for { a <- get[String] _ <- modify[String](_ + "1"

How does this State monad code works?

时光总嘲笑我的痴心妄想 提交于 2019-12-01 06:43:48
This code is from this article I've been able to follow it until this part. module Test where type State = Int data ST a = S (State -> (a, State)) apply :: ST a -> State -> (a,State) apply (S f) x = f x fresh = S (\n -> (n, n+1)) instance Monad ST where -- return :: a -> ST a return x = S (\s -> (x,s)) -- (>>=) :: ST a -> (a -> ST b) -> ST b st >>= f = S (\s -> let (x,s') = apply st s in apply (f x) s') data Tree a = Leaf a | Node (Tree a) (Tree a) deriving (Show) mlabel :: Tree a -> ST (Tree (a,Int)) -- THIS IS THE PART I DON'T UNDERSTAND: mlabel (Leaf x) = do n <- fresh return (Leaf (x,n))

Tidying up Monads - turning application of a monad transformer into newtype monad

末鹿安然 提交于 2019-11-30 19:23:47
I am trying to take e.g. ExceptT a (StateT A M) , for some concrete type A and monad M , and wrap them up into my new custom monads. First I identified that StateT A M appears often in other contexts and thus I decided it would be best to wrap that alone in a monad M1 and then wrap ExceptT a M1 into M2 . The desired property is to make M1 and M2 instances of MonadState and the class of M (lets assume it is called MyMonadClass ). Also M2 should be an instance of MonadError . First I started by simple type synonyms: type MyState = StateT A M type MyBranch a = ExceptT a MyState then I thought

Managing state - chapter 3 of SICP

安稳与你 提交于 2019-11-30 14:54:33
问题 I've been working through in Structure and Interpretation of Computer Programs and completing the exercises in Haskell. The first two chapters were fine (code at github) but Chapter 3 is making me think harder. It starts by talking about managing state, with the example of a bank account. They define a function make-withdraw by (define (make-withdraw balance) (lambda (amount) (if (>= balance amount) (begin (set! balance (- balance amount)) balance) "Insufficient funds"))) so that you can