I haven't managed to decipher all of your code, but here is the plan I would take for solving this problem. First, an English description of the plan: We need windows into the list of length n starting at each index. Make windows of arbitrary length. Truncate long windows to...
functional-programming,monads,state-monad
The purpose of the state monad is to hide the passing of state between functions. Let's take an example: The methods A and B need to use some state and mutate it, and B needs to use the state that A mutated. In a functional language with immutable data, this...
haskell,monad-transformers,state-monad
commands is polymorphic, it has a type variable, m, but commandNames :: [String] doesn't have any type variables. This means that (barring some built-in defaults) type inference won't be able to infer the type variable for commands. There are two things you can do. You can provide a type for...
haskell,monad-transformers,state-monad
I think that if you can write an instance of MonadState for your transformer you can use modify without the lift: instance Monad m => MonadState (ZipperT s m a) where ... I must confess I am not sure about what part of the state modify should affect, though. I've...
You can combine different monads using monad transformers - in this case StateT. You can use your existing code by changing the type signatures to use StateT: bigger, smaller :: Monad m => StateT Bound m () then you can write a function to run the game given a state...
scala,functional-programming,state-monad
This should be fine; the obvious approach will work: def applyState[A, R](a: A) = State(fn: Fn[A, R] => fn.apply(a)) val composedState = for { firstResult <- applyState[Int, Thingy](2) secondResult <- applyState[Int, Thingy](4) } yield secondResult composedState.run(new Fn[Int, Thingy]{...}) ...
haskell,lazy-evaluation,state-monad
According to the monad identity laws, return a >>= const b = const b a = b Thus in particular, return undefined >>= const b = b If the >>= operation were strict in the result value, that would break this law, so you shouldn't do that. Suppose you instead...
I think that the monad instance under discussion does preserve State structure and that the tuple is throwing you for a loop when it should not. If we take a step back and ask what signature should bind have in order to match up with our monad expectations, we get...
haskell,monads,monad-transformers,state-monad
Measure is already defined in a following way: newtype Measure a = Measure { unMeasure :: [Cond] -> Sampler (a, [Cond]) } You can see that there is no place to store your Int, so you cannot make it a proper instance of MonadState. If you want to extend Measure...
haskell,monad-transformers,state-monad
Update: I wasn't able to reproduce the issue, different GHC versions might optimize code differently. Nevertheless some ideas below: The forced evaluation you added doesn't do anything - it's never executed, as problem runs indefinitely. The reason why adding WriterT to the stack causes the memory leak could be precisely...
haskell,asynchronous,f#,state-monad
Basically there are 2 approaches to combine monads: use monad transformers or create a custom monad by hand which combines both monads. Since there are no native type classes in F# you usually end up with the latter solution, but you can use FsControl to combine Async and State a-la...
haskell,ffi,mutable,state-monad
Based on my comment, I'll give a short example of how this can be done. First start with your basic C module. typedef struct { int bar; int baz; } foo ; foo * newFoo (); void freeFoo (foo * ) ; int readBar ( foo * ) ; int...
haskell,monads,monad-transformers,state-monad
Your CheckerError monad is very similar to the Either monad. I will use the Either monad (and its monad transformer counterpart ErrorT) in my answer. There is a subtlety with monad trasformers: order matters. Effects in the "inner" monad have primacy over effects caused by the "outer" layers. Consider these...
haskell,monads,moving-average,state-monad,recurrence-relation
Updated answer... Define: combine :: [ a -> State s a ] -> a -> State [s] a combine fs a = state $ \ys -> let zs = zipWith (\f y a -> runState (f a) y) fs ys pairs = chain a zs as' = map fst pairs...
haskell,monad-transformers,state-monad,free-monad
Look at the type signature for runTest: it returns a value in a monad m as long as m is a MonadState FakeDB. So when you call runTest you need to bind it into such a monad, which IO is not (that's what the error message is telling you). Try...
haskell,random,monads,state-monad
Since you call put in nextWeightedRandom, the state gets updated on each iteration. So each time you call randomR ... g, it is using the generator output by the previous iteration. In fact, the question itself, "isn't evalState getting run over a bunch different copies of the same generator" is...
The type of (>>=) is: Monad m => m a -> (a -> m b) -> m b Since you are writing an instance for SM, the type of bind in your instance is therefore SM a -> (a -> SM b) -> SM b Notice that both a and...
haskell,monads,monad-transformers,state-monad,maybe
The type I would recommend using is: StateT [Int] Maybe Int A really simple way to use Maybe/MaybeT is to just call mzero whenever you want to fail and mplus whenever you want to recover from a failed computation. This works even if they are layered within other monad transformers....
Instead of trying to keep track of, whether a Computation is pure, before executing it, I took the approach to make it abort if it tries to execute IO but isn't allowed to. (Like danidiaz said, static analysis is hard on Monads.) To avoid executing the IO actions, the Computation...
haskell,cabal,type-inference,state-monad
I ended up just making a file at Game/Monad.hs containing: module Game.Monad (execStateT, MonadState, MonadIO) where import Control.Monad.State Then replaced my import Control.State.Monad with import Game.Monad in GreedyInference.hs. The file then compiled without the error. So I think @Rufflewind in the comments was on the right track - the mtl...
haskell,monad-transformers,state-monad,threepenny-gui
StateT won't work in this case. The problem is that you need the state of your counter to persist between invocations of the button callback. Since the callback (and startGUI as well) produce UI actions, any StateT computation to be ran using them has to be self-contained, so that you...
scalaz,state-monad,st-monad,starray,scala-cats
Here is a more or less direct translation from the Haskell version you linked that uses a mutable STArray. The Scalaz STArray doesn't have an exact equivalent of the listArray function, so I've made one up. Otherwise, it's a straightforward transliteration: import scalaz._ import scalaz.effect.{ST, STArray} import ST._ import State._...
scala,haskell,scalaz,monad-transformers,state-monad
The problem is that the modify you get with the usual imports is from State, and isn't going to help you with StateT. It's a good idea to start with the Haskell type signature: test3 :: (MonadState [Char] m, MonadState s (t m), MonadTrans t, Num s) => t m...