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

Generating new and different random lists in Haskell (without IO)?

This is very much a newbie question, and although I can find partial answers, I'm still having difficulty getting the whole thing to work. I have a collection of functions in a module with work with a particular data type I've designed. And…
Alasdair
  • 1,300
  • 4
  • 16
  • 28
4
votes
1 answer

Why does my MaybeT (State ) () ignore state changes?

Short version: when I use runMaybeT and then runState on a monad of type MaybeT (State ) (), it looks like no state changes occur even though the Maybe result is equal to Just (). Why? Full version: I'm writing a program to solve the Towers of…
Alex
  • 780
  • 4
  • 12
4
votes
1 answer

Implementation of flatMap() for State transition

Exercise 6.8, Chiusano and Bjarnason, Functional Programming in Scala, p. 87 asks how one might implement flatMap() for the following trait: trait RNG { def nextInt: (Int, RNG) } type Rand[+A] = RNG => (A, RNG) The answer key gives the following…
Ben Weaver
  • 960
  • 1
  • 8
  • 18
4
votes
1 answer

Haskell - How would I run a list of state monads?

I'm somewhat new to Haskell and I'm having some trouble with the State monad. I have created the following types. Stat a has a monoid, functor, applicative and monad instance created for it. The "main" type in my program is creature and it has many…
Nick Acosta
  • 1,890
  • 17
  • 19
4
votes
1 answer

confusion over the passing of State monad in Haskell

In Haskell the State is monad is passed around to extract and store state. And in the two following examples, both pass the State monad using >>, and a close verification (by function inlining and reduction) confirms that the state is indeed passed…
Quentin Liu
  • 141
  • 1
  • 10
4
votes
1 answer

How does the state monad bind into outer context

I am trying to understand the State monad and I am mighty confused I must admit. I created a Computational Expression and added a lots of print statements so I can follow up who gets called when. type State<'st,'a> = | Ok of 'a * 'st |…
robkuz
  • 9,488
  • 5
  • 29
  • 50
4
votes
2 answers

How to zoom a monad transformer?

thanks again for the help! I'm making extensive use of the E. Kmett's Lens library, to avoid X/Y problems I'll explain a bit of context. I'm working on an extensible text editor and want to provide extension writers with a monad DSL, an Alteration…
Chris Penner
  • 1,881
  • 11
  • 15
4
votes
0 answers

Coroutine with StateT and ST and IO

having some trouble with a group of monads I'm trying to combine. I'm using monad-coroutine, State and lens (as I have deeply nested state). I had an initial approach where there was a working solution. The main point here is that I can request to…
ksaveljev
  • 550
  • 2
  • 16
4
votes
1 answer

State monads: trading one pattern for another?

So I'm writing a game in Haskell, and I'm expressing a player's turn as a series of state-altering functions that correlate to various turn phases. Originally, this looks something like: let game' = phase1 game game'' = phase2 game' --…
J Cooper
  • 16,891
  • 12
  • 65
  • 110
4
votes
1 answer

Haskell: How to combine scalar and monadic values?

Currently I'm fiddling around with a haskell problem concerning an 2D ST Array and recursion. Given a 2D position and an array of directions, I wrote a line returning a list of all resulting points inside the array: let cellsAround = [resPt | dir…
Hennes
  • 1,340
  • 1
  • 10
  • 26
4
votes
1 answer

scalaz.State stack overflow in deep monadic loop

I'm experimenting with different implementation of Depth-First Search with scalaz. This traverse should handle wide along with deep tree-like structures. Main idea - subordinate elements should be generated according to some "state". For example set…
Odomontois
  • 15,918
  • 2
  • 36
  • 71
4
votes
4 answers

Stateful computation with different types of short-circuit (Maybe, Either)

I am trying to find the most elegant way of converting the following stateful imperative piece of code to pure functional representation (preferably in Haskell to use abstraction that its Monad implementation offers). However I am not yet good at…
4
votes
3 answers

How does this State monad code works?

This code is from this article I've been able to follow it until this part. module Test where type State = Int data ST a = S (State -> (a, State)) apply :: ST a -> State -> (a,State) apply (S f) x = f x fresh = S (\n -> (n,…
user1685095
  • 5,787
  • 9
  • 51
  • 100
4
votes
3 answers

Constructing minimal Haskell example on error-handling in the State Monad

I'm twisting my brain into knots trying to understand how to combine the State monad with Maybe. Let's start with a concrete (and intentionally trivial/unnecessary) example in which we use a State monad to find the sum of a list of numbers: import…
iceman
  • 2,020
  • 2
  • 17
  • 24
4
votes
5 answers

Is there an idiomatic way to do implicit local state in OCaml?

I want to write some code that builds a thing using some local state. For example, consider the following code that uses local state to generate sequential integers: type state = int ref let uniqueId : (state -> int) = fun s -> incr s; !s let…
hugomg
  • 68,213
  • 24
  • 160
  • 246