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
12
votes
3 answers

Scala and State Monad

I have been trying to understand the State Monad. Not so much how it is used, though that is not always easy to find, either. But every discussion I find of the State Monad has basically the same information and there is always something I don't…
melston
  • 2,198
  • 22
  • 39
12
votes
2 answers

Haskell help to understand this State monad code: where is runState defined?

I am new to Haskell and trying to understand monads. I am going thru this code Putting it here for quick reference newtype State s a = State { runState :: s -> (a,s) } instance Monad (State s) where return a = State $ \s -> (a, s) State act…
mntk123
  • 905
  • 6
  • 18
12
votes
1 answer

Stricter Strict State Monad

The strict state monad is defined using: m >>= k = State $ \s -> case runState m s of (a, s') -> runState (k a) s' But this can still leak memory, because a and s' are left unevaluated. For example, we might have a function f that takes a…
yong
  • 3,583
  • 16
  • 32
12
votes
4 answers

State Monad with multiple state values

Consider the following: do x1 <- new 2 set x1 3 x2 <- get x1 y1 <- new 10 set y1 20 y2 <- get y1 return (x2 + y2) I want this to result in 23. Is there a way to implement something like this in pure Haskell, and if so how? I…
Clinton
  • 22,361
  • 15
  • 67
  • 163
12
votes
2 answers

A parallel monad map in Haskell? Something like parMapM?

I'm looking for a way to run two computations in parallel in the ST-Monad. I am building a rather large array (using STUArray) and I would like to do it in parallel. So far I've found this and this Q&A here on stackoverflow, however the first does…
scravy
  • 11,904
  • 14
  • 72
  • 127
12
votes
3 answers

Combining monads in Haskell

I am trying to write a Spider Solitaire player as a Haskell learning exercise. My main function will call a playGame function once for each game (using mapM), passing in the game number and a random generator (StdGen). The playGame function should…
Ralph
  • 31,584
  • 38
  • 145
  • 282
11
votes
1 answer

Real life and useful examples of Reverse State monad

Reverse State monad is really nice and mind blowing example of Haskell language's expressiveness and lazy evaluation. But it's not that easy to understand this monad. Moreover, it's really hard to find some convincing real life example of what you…
Shersh
  • 9,019
  • 3
  • 33
  • 61
11
votes
6 answers

How can I write a state monad that does error handling as well?

I need to write a state monad that can also support error handling. I was thinking of using the Either monad for this purpose because it can also provide details about what caused the error. I found a definition for a state monad using the Maybe…
HaskellNoob
  • 133
  • 1
  • 5
11
votes
2 answers

What is the difference between `ioToST` and `unsafeIOToST` from GHC.IO

What can the differences and intended uses be for ioToST and unsafeSTToIO defined in GHC.IO? -- --------------------------------------------------------------------------- -- Coercions between IO and ST -- | A monad transformer embedding strict…
imz -- Ivan Zakharyaschev
  • 4,921
  • 6
  • 53
  • 104
11
votes
1 answer

stacking StateT in scalaz

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…
arya
  • 946
  • 5
  • 14
11
votes
1 answer

How to deal with application state in Gtk2Hs

Trying to learn to write applications with Gtk2Hs I'm getting difficulties bridging the gap between the event driven Gtk2HS and the persistent state of my model. So to simplify, lets say that I have this simple application module Main where import…
tonicebrian
  • 4,715
  • 5
  • 41
  • 65
11
votes
2 answers

Managing state - chapter 3 of SICP

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…
Chris Taylor
  • 46,912
  • 15
  • 110
  • 154
10
votes
2 answers

Tips for more elegant code with monads?

I finally got a hold on how to use monads (don't know if I understand them...), but my code is never very elegant. I guess is from a lack of grip on how all those functions on Control.Monad can really help. So I'd thought it would be nice to ask for…
Rafael S. Calsaverini
  • 13,582
  • 19
  • 75
  • 132
10
votes
1 answer

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

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…
jakubdaniel
  • 2,233
  • 1
  • 13
  • 20
10
votes
3 answers

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

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
ais
  • 2,514
  • 2
  • 17
  • 24
1 2
3
26 27