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
2 answers

Monad facade for a MonadState in Haskell

Background: I'm creating a game with a stateful monad for reading and writing changes to the global state of the game. I would like to divide my game into components, such as "Characters", providing a domain specific way for these components to…
1
vote
2 answers

I need to compute as many times as remaining elements in the state in a State Monad

I am studying this example of a stack implemented with the State monad. It uses the state monad implementation in scalaz. The example has been taken from Eugene Yokota post http://eed3si9n.com/learning-scalaz/State.html import scalaz._ import…
juanpavergara
  • 1,511
  • 1
  • 11
  • 22
1
vote
3 answers

How to apply stateful computation to a list?

Let's imagine a dummy subset of Brainf*ck: + increments the counter - decrements the counter A simple program: program = "++++--" -- should evaluate to 2 And a stateful evaluation function: eval :: Char -> State Int Char eval '+' = do x <- get …
Raphaël Mor
  • 356
  • 2
  • 14
1
vote
1 answer

How do I use a persistent State monad with Spock?

I'm just starting out with haskell and I'm having issues with a basic "echo" REST server. Spock looked like a nice starting place for a REST server, and I though I got the basics of the State monad, but I'm having issues understanding how to put a…
ReaperUnreal
  • 970
  • 7
  • 19
1
vote
1 answer

Random / State workflow in F#

I'm trying to wrap my head around mon-, err, workflows in F# and while I think that I have a pretty solid understanding of the basic "Maybe" workflow, trying to implement a state workflow to generate random numbers has really got me stumped. My…
monoceres
  • 4,722
  • 4
  • 38
  • 63
1
vote
1 answer

Monad escaping inside a StateT context

I am trying to get back a value that is in a json feed (via Aeson) directly inside a StateT stacked on IO: {-# LANGUAGE DeriveGeneric #-} module MyFeed where import Data.Aeson import Network.URI (parseURI, URI(..)) import Data.Maybe…
Randomize
  • 8,651
  • 18
  • 78
  • 133
1
vote
1 answer

Why does this list contain more than one distinct value?

I have code that looks like: import qualified Data.Vector as V import System.Random import Control.Monad.State.Lazy nextWeightedRandom :: State (StdGen, V.Vector Int) Int nextWeightedRandom = do (g, fs) <- get let (i, g') = randomR (0, V.length…
Patrick Collins
  • 10,306
  • 5
  • 30
  • 69
1
vote
1 answer

There is bug in calculating Fibonacci numbers - Haskell

I solve different problems in some judging systems. Today I want to calculate Fibonacci numbers with State Monad. My code works well and it pass all my tests. But there is some error (one test is failed), which I can not determine. My code is: fib…
Denis
  • 3,595
  • 12
  • 52
  • 86
1
vote
2 answers

Cleaning up mutable array code

I'm trying to solve the MINFREE problem in "Pearls of Functional Algorithm Design" using mutable arrays. The problem is as follows: given a list of n natural numbers, find the smallest natural number that does not appear in the list. I'm pretty sure…
Patrick Collins
  • 10,306
  • 5
  • 30
  • 69
1
vote
0 answers

How do you define your own state monad using scalaz?

So I'm trying to define my own state monad by extending the Monad trait in scalaz. I know I'm reinventing the wheel, but I'm trying to learn more about scala and scalaz. My code is as follows: package pure import scalaz._, Scalaz._ object StateUtil…
rialmat
  • 133
  • 1
  • 6
1
vote
1 answer

A monad of tuples (monad, state)?

I am trying to write a Haskell "number-guessing" game program, using Monads, but I am stucked: I tried the simple state monad: data SM a = SMN (S -> (a, S)) instance Monad SM where SMN c1 >>= fc2 = SMN (\s0 -> let (r, s1) = c1 s0 in …
awllower
  • 571
  • 1
  • 9
  • 21
1
vote
1 answer

How to update the state with async requests using the state monad?

I'm doing an app using F# and I wonder if I could a) Keep the state under the State computation expression (using FSharpx's implementation) and b) be able to modify the state asynchronously (handling the user's input). I believe I know how to handle…
Lay González
  • 2,901
  • 21
  • 41
1
vote
1 answer

extracting names from a list of (name,handler) pairs

Here is a simple example using haskeline with the StateT transformer to create a stateful input command loop: {-# LANGUAGE NoMonomorphismRestriction #-} {-# LANGUAGE FlexibleContexts #-} import Control.Monad.State.Strict import Control.Monad.Trans…
ErikR
  • 51,541
  • 9
  • 73
  • 124
1
vote
1 answer

Haskell deducing constraints works in cabal package, but not when importing from package

I wrote a cabal package with a number of working examples. However, when I copy one of these examples out of the Examples directory and try to run it, I get the error below: $ cabal sandbox init $ cabal add-source deckbuild/ $ cabal install…
cronburg
  • 892
  • 1
  • 8
  • 24
1
vote
1 answer

Haskell State monadic function using recursion

TL:DR: Is there a way to do example 3 without passing an argument I'm trying to understand the state monad in haskell (Control.Monad.State). I made an extremely simple function: Example 1 example :: State Int Int example = do e <- get put…
Obrazi
  • 77
  • 7