Questions tagged [state-monad]

A monad allowing state information to be attached to calculations

A state monad allows a programmer to attach state information of any type to a calculation. Given any value type, the corresponding type in the state monad is a function which accepts a state, then outputs a new state along with a return value. Wikipedia has a brief overview.

399 questions
99
votes
3 answers

Difference between State, ST, IORef, and MVar

I am working through Write Yourself a Scheme in 48 Hours (I'm up to about 85hrs) and I've gotten to the part about Adding Variables and Assignments. There is a big conceptual jump in this chapter, and I wish it had been done in two steps with a good…
John F. Miller
  • 26,961
  • 10
  • 71
  • 121
77
votes
3 answers

Scalaz state monad examples

I haven't seen many examples of the scalaz state monad. There is this example but it is hard to understand and there is only one other question on stack overflow it seems. I'm going to post a few examples I've played with but I would welcome…
huynhjl
  • 41,520
  • 14
  • 105
  • 158
62
votes
8 answers

Use of Haskell state monad a code smell?

God I hate the term "code smell", but I can't think of anything more accurate. I'm designing a high-level language & compiler to Whitespace in my spare time to learn about compiler construction, language design, and functional programming (compiler…
Cybis
  • 9,773
  • 2
  • 36
  • 37
48
votes
5 answers

ST Monad == code smell?

I'm working on implementing the UCT algorithm in Haskell, which requires a fair amount of data juggling. Without getting into too much detail, it's a simulation algorithm where, at each "step," a leaf node in the search tree is selected based on…
mergeconflict
  • 8,156
  • 34
  • 63
23
votes
4 answers

Confusion over the State Monad code on "Learn you a Haskell"

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…
byrondrossos
  • 2,107
  • 1
  • 15
  • 19
22
votes
2 answers

Is the Writer Monad effectively the same as the State Monad?

There's a great tutorial here that seems to suggest to me that the Writer Monad is basically a special case tuple object that does operations on behalf of (A,B). The writer accumulates values on the left (which is A) and that A has a…
jordan3
  • 877
  • 5
  • 13
21
votes
1 answer

Baffled by the Arrow in Do Statements in Haskell

I am working on understanding the State monad and have written two simple versions of the famous fibonacci to memoize the function. The one with let in the body runs very slowly. The one with <- runs very fast. My question: Why? Is let causing full…
dsagman
  • 481
  • 1
  • 8
20
votes
1 answer

Layering State with Either in scalaz

In Integrating State with Either (slide 88), given the pattern of State layered under Either, is there a recommended approach for adding another type of state, e.g., logging via something like Writer? It seems the new state has to live between the…
Sim
  • 13,147
  • 9
  • 66
  • 95
19
votes
3 answers

Combining StateT and State monads

Lets say I have a function f :: State [Int] Int and a function: g :: StateT [Int] IO Int I want to use f in g and pass the state between them. Is there a library function for StateT (return . runState f)? Or in general, given a monad transformer…
HaskellElephant
  • 9,819
  • 4
  • 38
  • 67
19
votes
3 answers

How do I avoid referring to all state variables when updating only a few?

An idiom I use for composing a couple of procedures (with memory) is as follows: p1 :: State (Int, String) () p1 = do (a, b) <- get ... do something ... put (a', b) p2 :: State (Int, String) () p2 = do (a, b) <- get ... do…
xzhu
  • 5,675
  • 4
  • 32
  • 52
18
votes
2 answers

Confusion about StateT, State and MonadState

I'm utterly confused between newtype StateT s m a = StateT {runStateT :: s -> m (a, s)} and type State s = StateT s Identity and class Monad m => MonadState s m | m -> s
hgiesel
  • 5,430
  • 2
  • 29
  • 56
18
votes
3 answers

Is it possible to implement `(Applicative m) => Applicative (StateT s m)`?

I'm currently working on Data.Fresh and Control.Monad.Trans.Fresh, which resp. define an interface for generating fresh variables, and a monad transformer which implements this interface. I initially thought it would be possible to implement the…
user824425
17
votes
1 answer

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

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…
haskelline
  • 1,116
  • 7
  • 15
17
votes
4 answers

Haskell: How to write interactive interpreter on top of a State monad?

We're working on a model filesystem that uses a state monad internally. We have a type class with operations like these: class Monad m => FS m where isDirectory :: Path -> m Bool children :: Path -> m [Path] ... We're working on a…
Norman Ramsey
  • 198,648
  • 61
  • 360
  • 533
17
votes
3 answers

State Monad, sequences of random numbers and monadic code

I'm trying to grasp the State Monad and with this purpose I wanted to write a monadic code that would generate a sequence of random numbers using a Linear Congruential Generator (probably not good, but my intention is just to learn the State Monad,…
Rafael S. Calsaverini
  • 13,582
  • 19
  • 75
  • 132
1
2 3
26 27