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
8
votes
4 answers

Why isn't the state monad traversable?

Intuitively, it seems to me that one could use traverse with a state monad, for example: traverse (\a -> [a, a+1]) (state (\s -> (1, s + 1))) = [state (\s -> (1, s + 1), state (\s -> (2, s + 1)] But the state monad doesn't implement the…
Guillaume Chérel
  • 1,478
  • 8
  • 17
8
votes
3 answers

How to use lenses to look up a value in a map, increase it or set it to a default value

While working on a state called AppState I want keep track of the number of, say, instances. These instances have distinct ids of type InstanceId. Therefore my state look likes this import Control.Lens data AppState = AppState { -- ... …
ruben.moor
  • 1,876
  • 15
  • 27
8
votes
4 answers

Guess My Number, a monadic headache

To test my skills in Haskell, I decided I wanted to implement the very first game you find in Land of Lisp / Realm of Racket. The "Guess My Number" game. The game relies on mutable state to run, as it constantly has to update upper and lower bounds…
Electric Coffee
  • 11,733
  • 9
  • 70
  • 131
8
votes
2 answers

Why does ParsecT type have 'u' argument?

Documentation for the parsec package states that u argument is used to carry some user state through monadic computation. But the same functionality can be achieved by basing ParsecT monad transformer on State monad. So if my parser is not stateful,…
arrowd
  • 33,231
  • 8
  • 79
  • 110
8
votes
4 answers

How can I parameterise my Haskell functions?

I hope my terminology is correct here - if not, feel free to edit anything. I'm using Haskell as a helper-language whilst writing a paper on combinatorial game theory - i.e., all of my functions just juggle numbers and help me to come to a solution…
Allan
  • 149
  • 5
8
votes
2 answers

Recursive state monad for accumulating a value while building a list?

I'm totally new to Haskell so apologies if the question is silly. What I want to do is recursively build a list while at the same time building up an accumulated value based on the recursive calls. This is for a problem I'm doing for a Coursera…
Russell
  • 12,261
  • 4
  • 52
  • 75
8
votes
1 answer

Stuck in the State Monad

I want to create a graph structure using an IntMap of nodes and unique keys. This topic has been covered well here and here. I understand how the state monad works by basically wrapping a function of state -> (val,state) in a newtype so we can…
MFlamer
  • 2,340
  • 2
  • 18
  • 25
7
votes
2 answers

How can I combine the CheckingFuelMonad with a State monad in Hoopl?

I am using the Hoopl library and would like to carry some state around while rewriting. The rewrite functions are polymorphic regarding the monad used, but I cannot figure out how to combine a State monad with one of the library's Fuel monads. Below…
Justin Bailey
  • 1,487
  • 11
  • 15
7
votes
1 answer

Combining State monad with Either-style error propagation

I am fairly new to Haskell. I am trying to combine the State monad with error propagation by treating Either as a monad. I would like to recurse over an abstract syntax tree (for example, for writing an interpreter over statements and expressions)…
blandish
  • 1,245
  • 1
  • 9
  • 11
7
votes
2 answers

What is the name of this Monad Stack function?

I've got a bunch of stateful functions inside a State monad. At one point in the program there needs to be some IO actions so I've wrapped IO inside a StateT getting a pair of types like this: mostfunctions :: State Sometype a toplevel :: StateT…
Andrew
  • 2,943
  • 18
  • 23
7
votes
1 answer

Is there a “dual” to zooming?

zoom allows us to use a state action that only uses some state variables, in a context where more variables are actually defined. {-# LANGUAGE TemplateHaskell #-} import Control.Lens import Control.Monad.Trans.State import…
leftaroundabout
  • 117,950
  • 5
  • 174
  • 319
7
votes
1 answer

State transformations with a shapeless State monad

Scalaz State monad's modify has the following signature: def modify[S](f: S => S): State[S, Unit] This allows the state to be replaced by state of the same type, which does not work well when the state includes a shapeless value such as a Record…
Sim
  • 13,147
  • 9
  • 66
  • 95
7
votes
3 answers

One more time...can I have an example of state monad that does what I want?

I'm trying to understand the actual need for the reader and/or state monads. All the examples I've seen (including many on stackoverflow as I've hunted for suitable examples I can use and in various books and blog articles) are of the form (pseudo…
David
  • 5,991
  • 5
  • 33
  • 39
7
votes
1 answer

How to handle nested structure when traversing with state monad

I have a nested structures which I'm converting to XML using a scalaz state monad. This works well until I have to deal with multi-level nested structures. Here is a simplified example similar to what I'm doing. Given the following ADT: sealed trait…
huynhjl
  • 41,520
  • 14
  • 105
  • 158
6
votes
2 answers

Different, interacting levels of state in haskell

I'm emulating a 4 bit microprocessor. I need to keep track of the registers, the memory and the running output (bonus points for also having a fetch-execute cycle counter). I've managed to do this without monads, but it feels messy passing around…
TheIronKnuckle
  • 7,224
  • 4
  • 33
  • 56