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

Generating random strings from a string-pool using QuickCheck

Consider the problem of generating strings out our a set of possible strings, in such a way that once a string is chosen, it cannot be repeated again. For this task I would like to use QuickCheck's Gen functions. If I look at the type of the…
Damian Nadales
  • 4,907
  • 1
  • 21
  • 34
0
votes
1 answer

Missing Monadstate instance

I am attempting to build a slackbot using this library: https://hackage.haskell.org/package/slack-api, just to learn a little bit more haskell, and hopefully, finally understand monads -_-. I then have the following types: data BotState = BotState …
Abraham P
  • 15,029
  • 13
  • 58
  • 126
0
votes
1 answer

adding state monad to the haskell kv database

Playing with an old code review here simple-db. and get stuck when I added State Monad into it to further reduce the code. I somehow feel I am not doing it correctly. repl cmd = getCmd >>= execCmd cmd >>= displayResult >>= continue change the…
zinking
  • 5,561
  • 5
  • 49
  • 81
0
votes
1 answer

combining IO monad with State Monad in example

I created simple evaluator for statements. I would like to do it using transformers - mix IO monad with State. Could somebody explain how to do it ? It is something that I can't deal with it - transformers. execStmt :: Stmt -> State (Map.Map…
user6023611
0
votes
1 answer

Using MonadRandom with MonadState

I have this bit of code: import Data.Random import Control.Monad.State foo :: s -> StateT s RVar () foo s = do p <- lift $ (uniform 0 1 :: RVar Double) if p > 0.5 then put s else return () And I would like to refactor its signature to be of…
0
votes
2 answers

State monad vs atom in Clojure

Does state monad make any sense in Clojure? If so, I would appreciate cases where state monad will be more suitable than mutating Clojure atoms via swap!s or reset!s.
OlegTheCat
  • 4,443
  • 16
  • 24
0
votes
1 answer

Distracted While Trying to Understand State Monad

I'm trying to understand and get comfortable with the State Monad so I'm basically following along by copying a preexisting example. The 'State' is just a door that opens if it's Pushed when closed and closes when Pulled if it's open. If it's Pushed…
0
votes
2 answers

Haskell: return value, state, and state monads

I'm making a function that does (a->Bool) -> [a] -> State [a] [a] and I was wondering if there was a simple way to go through the list storing "failed" items that didn't pass the predicate as the state and the "passing" items as the value that…
0
votes
3 answers

Is it possible to emulate the behaviour of StateT without the use of custom types?

If we have the following two functions, add and subtract, it is simple to chain them to run a series of calculations on an input: add :: Int -> State Int () add n = state $ \x -> ((),x+n) subtract :: Int -> State Int () subtract n = state $ \x ->…
lightandlight
  • 1,345
  • 3
  • 10
  • 24
0
votes
2 answers

Custom MonadState instance

When I do: cabal sandbox init cabal update cabal install hakaru cabal repl λ> :l simple.hs λ> sample test [] with simple.hs containing: {-# LANGUAGE MultiParamTypeClasses #-} import Language.Hakaru.ImportanceSampler import…
cronburg
  • 892
  • 1
  • 8
  • 24
0
votes
1 answer

Can't match expected type and stuck on assignment about State Monads

For an assignment for FP we have to write a function that runs a state monadic computation given an initial state, and then returns the computed value and the number of operations counted. Counts looks like this: data Counts = Counts { binds …
user3086758
0
votes
1 answer

Creating a `State [Int] Int`

Reading through Learn You a Haskell, I'm trying to construct a Stack [Int] Int: ghci>import Control.Monad.State ghci> let stack = state ([1,2,3]) (1) :: State [Int] Int :58:20: Couldn't match expected type `s0 -> (State [Int] Int,…
Kevin Meredith
  • 41,036
  • 63
  • 209
  • 384
0
votes
2 answers

piping data through Q promises chain

I'm chaining Q promises: Q(initialCall).then(someOtherCallThatUsesResultsFromPreviousResults) A call usually means a promisified node.js http.get call to an external REST api. The path is constructed using information from previous call. At the…
sumek
  • 26,495
  • 13
  • 56
  • 75
0
votes
2 answers

How can this haskell rolling sum implementation be improved?

How can I improve the the following rolling sum implementation? type Buffer = State BufferState (Maybe Double) type BufferState = ( [Double] , Int, Int ) -- circular buffer buff :: Double -> Buffer buff newVal = do ( list, ptr, len) <- get …
Dave Anderson
  • 667
  • 1
  • 7
  • 14
0
votes
0 answers

cabal build missing Control.Monad.State even though mtl is in build-depends

I'm trying to build my own cabal package, where one of the modules use Control.Monad.State, but I get the following error: executable/Example.hs:5:8: Could not find module `Control.Monad.State' It is a member of the hidden package…
jswetzen
  • 688
  • 1
  • 4
  • 15
1 2 3
26
27