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

Recursively update a State Monad

this question is related to this question I have a state monad. An object provides an update function as in the OOD strategy pattern. The choice of having a object is that in real, production code, the class provides an array of operations, all…
NoIdeaHowToFixThis
  • 4,484
  • 2
  • 34
  • 69
0
votes
1 answer

Using Maybe with State Monad

I'm trying to implement a FIFO Queue in Haskell with push/pop/peek operations, and this is what I got so far. data Queue a = Queue { inbox :: [a], outbox :: [a] } deriving (Eq, Show) push :: a -> Queue a -> Queue a push e (Queue inb out) =…
rafalio
  • 3,928
  • 4
  • 30
  • 33
0
votes
1 answer

State monad and gtk2hs

I am trying to retain some state on my GUI application so that I may be able to construct a list of values from user input. But I have trouble understanding the State monad so... Here is some test code to illustrate what I want (it is obvious it…
Sarfraz
  • 225
  • 2
  • 10
0
votes
1 answer

Returning a value with a state monad

I'm new to Monads and Haskell in general and trying to understand how to return a value when using them. My code looks something like the following: foo :: A -> B foo a = do b <- fooC a (C 0) -- want to return just (B "b") fooC :: A ->…
Wesley Tansey
  • 4,555
  • 10
  • 42
  • 69
0
votes
4 answers

IDs from State Monad in Haskell

Possible Duplicate: Creating unique labels in Haskell I've got a datatype Person and some input data from which I will create the Persons. I'd like to have each Person have its own ID (let's say integers [0..]). I could do this with recursion,…
Martin Janiczek
  • 2,996
  • 3
  • 24
  • 32
0
votes
1 answer

Side Effect Tracking in SSA

I am working on an optimizer for Java bytecode and decided to use SSA. However, most optimizations require all operations to be purely functional, so in order to handle side effects, I decided to add an extra opaque state parameter and return value…
Antimony
  • 37,781
  • 10
  • 100
  • 107
-1
votes
1 answer

Haskell State Monad Coordinate

I am trying to figure out how the state Monad works. I am trying to implement a function that gets two coordinates (x, y) and returns the x or y coordinate back. This should happen within the State Monad. data Coordin = Coordin {x,y :: Float} the…
-1
votes
1 answer

State where the resulting state is the argument provided and the value is unit

Exercise 23.8.2 in the haskell book asks me to construct a state like the following: put' :: s -> State s () put' s = undefined -- should act like: -- Prelude> runState (put "blah") "woot" -- ((),"blah") The only implementation I have gotten to…
The Unfun Cat
  • 29,987
  • 31
  • 114
  • 156
-4
votes
2 answers

how can i pop two elements from a stack and then push in stack them back as sum of values?

import Control.Monad.State type Stack = [Integer] pop :: State Stack Integer pop = state $ \(x:xs) -> (x, xs) push :: Integer -> State Stack () push x = state $ \xs -> ((), (x:xs)) main :: IO() main = print $ runState `enter code here`…
1 2 3
26
27