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
1
vote
1 answer

Best practice with Monad

I would like to know what can be considered as a best practice regarding the State monad. I'm also open to any other suggestion. I have a binary file to parse. It contains different header that need to be parsed in order to be able to read the…
mathk
  • 7,973
  • 6
  • 45
  • 74
1
vote
2 answers

choosing one of the nondeterministic choices

The following toy example computes nondeterministically a number by calling a function anyFunction, and then keeps only the even choices. How could I write a similar code that keeps the maximum choice instead the even choices? Do I need a different…
Bob
  • 1,713
  • 10
  • 23
1
vote
1 answer

Find word in 2d maze using State monad

If we have a board 4x4 with characters (which may repeat) and some random word we need to find if that word can be found on the board. Now this problem is rather easy to solve without using any fancy stuff. But from the learning perspective how can…
ksaveljev
  • 550
  • 2
  • 16
1
vote
2 answers

StateT and WX gui coexistance

usual wxHaskell program looks like main = do run gui gui = do .... .... gui must have type IO a, run has type IO a -> IO (), also there is some initialization routines in run. I'm tring to do following: data AppGlobals = AG {…
Vasiliy Stavenko
  • 1,174
  • 1
  • 12
  • 29
1
vote
1 answer

Why the author claims this function call not to be a recursive call?

The author claims that the next call is not a recursive call, but I don't get why. Source: http://courses.cms.caltech.edu/cs11/material/haskell/lectures/haskell_lecture_5.pdf
Lay González
  • 2,901
  • 21
  • 41
1
vote
1 answer

How to thread a state in the same State monad indefinitely in Haskell?

Beginning with Haskell and stuck at the State Monad ... So I am trying to come to grips with the State Monad in Haskell, and to understand it, I am writing a code to generate PRBS sequences. For people interested, it is described in the paper…
ssm
  • 5,277
  • 1
  • 24
  • 42
1
vote
2 answers

What are the advantages of a wrapped up the state monad?

This one may be a stupid one but, having a look at (Eliminating my explicit state passing via like, monads and stuff) type State<'s,'a> = State of ('s -> 'a * 's) type StateBuilder<'s>() = member x.Return v : State<'s,_> = State(fun s -> v,s) …
NoIdeaHowToFixThis
  • 4,484
  • 2
  • 34
  • 69
1
vote
1 answer

Execute monadic code from newly created monad

I currently have two monads who share the same types, implemented similar to a State monad: newtype FooRead a = FooRead { runFooRead :: Context -> (a,Context) } newtype FooWrite a = FooWrite { runFooWrite :: Context -> (a,Context) } The…
definelicht
  • 428
  • 2
  • 14
1
vote
2 answers

Scala state monad in for comprehension

I'm studying Michael Pilquist's excellent state monad lecture here. I get stuck at 54 min with two questions. If ofs is an Option[FollowerStats], what is the ? operator? I can't find a ternary operator on Option in Scala 2.10.2 How does the last…
Pengin
  • 4,692
  • 6
  • 36
  • 62
1
vote
3 answers

Can I use different workflows simultaneously in F#?

I need my state to be passed along while being able to chain functions with the maybe workflow. Is there a way for 2 workflows to share the same context? If no, what is the way of doing it? UPDATE: Well, I have a state that represents a segment of…
Trident D'Gao
  • 18,973
  • 19
  • 95
  • 159
1
vote
1 answer

How do bind, put and return interact here?

Given the following code rollDie :: GeneratorState Int rollDie = do generator <- get let (value, newGenerator) = randomR (1,6) generator put newGenerator return value I know I can translate it as: rollDie2 ::…
hernan
  • 572
  • 4
  • 10
1
vote
2 answers

How to convert a STArray to a List in Haskell?

Is there a function can do what the function arrayToList do: import Data.Array.ST import Control.Monad.ST genArray :: ST s [Int] genArray = do a <- new Array (0, 99) 0 :: ST s (STArray s Int Int) writeArray a 0 1 {- ... write something to…
RnMss
  • 3,696
  • 3
  • 28
  • 37
1
vote
1 answer

Stitching functions together using the ReaderT and StateT Monads

I am beginning with Haskell and I need help stitching functions together using the ReaderT and StateT Monads. Ideally, if that is at all possible, all functions would have the same signature (which I understand those Monads should help with) and…
Bruno Grieder
  • 28,128
  • 8
  • 69
  • 101
0
votes
3 answers

How to share state between two monads?

I am stuck with following monad problem: Let's say I have a standard monad State with state S = (LS, RS). I also have another monad: newtype StateP a = StateP {runP :: S -> (a, RS)} I want to perform some computation using StateP then merge state…
0
votes
1 answer

Extracting, manipulating and accumulating values within a list of monads in Haskell

I am trying to create a function that 'extracts' and evaluate the value of a each monad in a list of monads such that a final value - accumulated or so - can be returned. The signature for such a function would be something like accCurryingMonad ::…
Piskator
  • 605
  • 1
  • 9