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

Understanding the state argument in the State Monad

I'm trying so hard to wrap my head around the State Monad, and I do not understand the following: Given the implementation of return and (>>=), when you say State $ \s ->...., where does s come from? I mean, when you start performing >>= ... >>=,…
Bercovici Adrian
  • 8,794
  • 17
  • 73
  • 152
5
votes
1 answer

Monad transformer for inserts and total lookups on a Map?

I have a computation where I'm inserting values into a Map and then looking them up again. I know that I never use a key before inserting it, but using (!) freely makes me nervous anyway. I'm looking for a way to get a total lookup function that…
user11228628
  • 1,526
  • 1
  • 6
  • 17
5
votes
2 answers

Combine ST and List monads in Haskell

Using the StateT monad transformer, I can create the type StateT s [] a, which is isomorphic to s -> [(a, s)]. Now I would prefer to use the STT monad transformer instead, as I would like to have multiple mutable variables of different types, and…
dremodaris
  • 358
  • 1
  • 9
5
votes
3 answers

Generating a unique value in Haskell do-notation

To generate x86 assembly code, I have defined a custom type called X86: data X86 a = X86 { code :: String, counter :: Integer, value :: (X86 a -> a) } This type is used in do-notation like the following. This makes it easy to write templates for…
Ryan
  • 2,378
  • 1
  • 19
  • 29
5
votes
1 answer

How does `get` work in the CPS version of the State monad?

I am trying to understand continuation in general following this tutorial. However, I am having difficulties to understand following example in section 2.10: # let get () = shift (fun k -> fun state -> k state state) ;; get : unit => ’a =…
Jason Hu
  • 6,239
  • 1
  • 20
  • 41
5
votes
1 answer

State monads: Transitioning from one state type to another

Let's say we have a stack of monads with a state monad transformer as the outer most transformer like this one: -- | SEWT: Composition of State . Except . Writer monad transformers in that -- order where Writer is the innermost transformer. -- the…
Centril
  • 2,549
  • 1
  • 22
  • 30
5
votes
2 answers

When and Why do you use State Monads?

I can write a State Monad (in Scala/Java) and can pretty much follow the logic when I see others using it. I don't fully understand the problem it is solving. It is a monad wrapping the funciton S => (S,A). As such, when you nest functions that…
jordan3
  • 877
  • 5
  • 13
5
votes
2 answers

Stateful loop with different types of breaks

I am trying to convert the following stateful imperative code into Haskell. while (true) { while (get()) { if (put1()) { failImmediately(); } } if (put2()) { succeedImmediately(); } } Both the put1 and put2 read a state of…
jakubdaniel
  • 2,233
  • 1
  • 13
  • 20
5
votes
1 answer

Is a state monad with two state variable types (in and out) still a monad?

Haskell's state monad State s a forces me to keep the same type of s during the whole do block. But since the state monad is really just a function, what if I define it as State i o a = State (i -> (o, a))?. The return and bind functions would look…
Juan
  • 15,274
  • 23
  • 105
  • 187
5
votes
1 answer

Random number sequence un Haskell and State Monad, what am I doing wrong?

As part of my Haskell journey, I am implementing a raytracer and I need to be able to draw sequences of random numbers at several places in the code. Typically I would like to be able to get say 64 samples for each pixels and pixels are computed in…
overlii
  • 563
  • 6
  • 20
5
votes
1 answer

Difficulty with zoom and free monads

I am mucking around with free monads and lens, using the free monad to create my own version of the IO monad: data MyIO next = LogMsg String next | GetInput (String -> next) deriving (Functor) I am stacking this on top of a state monad…
Pubby
  • 51,882
  • 13
  • 139
  • 180
5
votes
2 answers

How does the state monad work? (without code explanation)

For the last number of months I've been taking some spare time here and there to read up on Monads. I haven't worked with a functional language since my University days. So I don't really remember Haskell, and certainly have no idea about Scalaz.…
qeadz
  • 1,476
  • 1
  • 9
  • 17
5
votes
4 answers

Implementing recurrence relations on State monads (in Haskell or Scala)

I am working on a new implementation of the operators in http://www.thalesians.com/archive/public/academic/finance/papers/Zumbach_2000.pdf EDIT: clearer explanation here: https://www.olseninvest.com/customer/pdf/paper/001207-emaOfEma.pdf Briefly,…
experquisite
  • 879
  • 5
  • 14
5
votes
2 answers

Haskell - is state monad a sign of imperative thinking?

I am writing a simple game - Tetris. For the first time in my life I'm using functional programming for that goal, as a language I chose Haskell. However, I'm tainted with OOP and imperative thinking and scared of unconsciously applying this mindset…
PL_kolek
  • 325
  • 3
  • 9
5
votes
2 answers

Searching for missing State Combinator for Lens

I currently have code that looks like this: do x <- use foo foo <~ runFoo x where foo is a Lens to a Foo field and runFoo :: MonadState m => Foo -> m Foo I think there should be a way to do this operation in one line, but I cannot find it. I…
John F. Miller
  • 26,961
  • 10
  • 71
  • 121