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
15
votes
1 answer

State monad in OCaml

I was trying to implement the state monad in OCaml (as an exercise). My implementation looks like this: module type MONAD_BUILDER = sig type 'a t val return : 'a -> 'a t val bind : 'a t -> ('a -> 'b t) -> 'b t end;; module MonadBuilder =…
Alex
  • 2,040
  • 2
  • 19
  • 29
15
votes
1 answer

Retrocausality in Haskell: From Tardis to RevState

The following program uses a backwards-traveling state as provided by the Tardis monad. {-# LANGUAGE RecursiveDo #-} import Control.Monad.Tardis lastOccurrence :: Int -> Tardis [Int] () Bool lastOccurrence x = mdo sendPast (x : xs) xs <-…
Bob
  • 1,713
  • 10
  • 23
15
votes
1 answer

Scala State monad - combining different state types

I'm wrapping my head around State monad. Trivial examples are easy to understand. I'm now moving to a real world case where the domain objects are composite. For example, with the following domain objects (they don't make much sense, just sheer…
ak.
  • 3,329
  • 3
  • 38
  • 50
15
votes
4 answers

What is the purpose of the state monad?

I am a JavaScript developer on a journey to up my skills in functional programming. I recently ran into a wall when it comes to managing state. When searching for a solution I stumbeled over the state monad in various articles and videos but I have…
Ludwig Magnusson
  • 13,964
  • 10
  • 38
  • 53
14
votes
1 answer

scalaz List[StateT].sequence - could not find implicit value for parameter n: scalaz.Applicative

I'm trying to figure out how to use StateT to combine two State state transformers based on a comment on my Scalaz state monad examples answer. It seems I'm very close but I got an issue when trying to apply sequence. import scalaz._ import…
huynhjl
  • 41,520
  • 14
  • 105
  • 158
14
votes
3 answers

Simulating interacting stateful objects in Haskell

I'm currently writing a Haskell program that involves simulating an abstract machine, which has internal state, takes input and gives output. I know how to implement this using the state monad, which results in much cleaner and more manageable…
N. Virgo
  • 7,970
  • 11
  • 44
  • 65
14
votes
2 answers

Haskell Monad Transformer Stack and Type Signatures

I am attempting to create a stack of monad transformers and am having trouble getting the correct type signatures for my functions. (I'm still pretty new to Haskell) The stack combines multiple StateT transformers since I have multiple states I need…
paul
  • 1,655
  • 11
  • 23
14
votes
4 answers

Has the Control.Monad.State API changed recently?

As a learning exercise, I'm trying to implement a heapsort in Haskell. I figured the State monad would be the right choice to do this, since heaps rely pretty heavily on moving data around inside a single structure (and do notation would be useful).…
Benjamin Hodgson
  • 42,952
  • 15
  • 108
  • 157
14
votes
3 answers

what is proper monad or sequence comprehension to both map and carry state across?

I'm writing a programming language interpreter. I have need of the right code idiom to both evaluate a sequence of expressions to get a sequence of their values, and propagate state from one evaluator to the next to the next as the evaluations take…
Mike Beckerle
  • 725
  • 5
  • 13
14
votes
3 answers

Haskell: map runST

I have a binding for a type [ST s (Int, [Int])] and I am trying to apply runST to each element using map as follows: name :: [ST s (Int, [Int])] --Of Course there is a real value here map runST name This gives me an error message Couldn't match…
Ra is dead
  • 569
  • 2
  • 18
13
votes
3 answers

Understanding the Reader monad

I'm reading PureScript by Example and got to the part introducing the Reader monad. The example goes like this: createUser :: Reader Permissions (Maybe User) createUser = do permissions <- ask if hasPermission "admin" permissions then map…
kaqqao
  • 12,984
  • 10
  • 64
  • 118
13
votes
1 answer

how can I decently add an "undo" functionality to State monads?

Say that I have a State monad, and I want to do some manipulations on the state and might want to undo the change in future. How in general can I do this decently? To give a concrete example, let's assume the state is just an Int, and the…
Javran
  • 3,394
  • 2
  • 23
  • 40
13
votes
3 answers

How does 'get' actually /get/ the initial state in Haskell?

I have a function: test :: String -> State String String test x = get >>= \test -> let test' = x ++ test in put test' >> get >>= \test2 -> put (test2 ++ x) >> return "test" I can pretty much understand what goes on throughout…
Rayne
  • 31,473
  • 17
  • 86
  • 101
12
votes
1 answer

Basic Scalaz State question

How do I use State to mimic the behaviour of List.zipWithIndex? What I have come up with so far (which doesn't work) is: def numberSA[A](list : List[A]) : State[Int, List[(A, Int)]] = list match { case x :: xs => (init[Int] <* modify((_:Int) + 1))…
oxbow_lakes
  • 133,303
  • 56
  • 317
  • 449
12
votes
3 answers

Updating a Big State Fast in Haskell

For my vector graphics library in Haskell I must carry around a rather big state: line stroke parameters, colors, clip path etc. I know two ways of doing this. Quoting a comment from Haskell-cafe: "I would suggest you either use a reader monad with…
DJS
  • 123
  • 1
  • 6
1
2
3
26 27