Menu
  • HOME
  • TAGS

How can this haskell rolling sum implementation be improved?

haskell,state-monad

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...

What is the purpose of the state monad?

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...

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

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...

Create my own state monad transformer module hiding underlying state monad

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...

Guess My Number, a monadic headache

haskell,state-monad,io-monad

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...

Can the State monad be used for self-modifying functions?

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]{...}) ...

Stricter Strict State Monad

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...

How does the state monad work? (without code explanation)

monads,state-monad

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...

Custom MonadState instance

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...

Infinite recursion in IO wrapped with StateT causing stack space overflow

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...

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

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...

How to implement actions in ST-monad with my own underlying representation (similarly to STRef or STArray) using simple techniques?

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...

How to preserve information when failing?

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...

Implementing recurrence relations on State monads (in Haskell or Scala)

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...

Using free monads to test Redis calls

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...

Why does this list contain more than one distinct value?

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...

A monad of tuples (monad, state)?

haskell,io,monads,state-monad

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...

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

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....

Haskell - Mixed stateful computations

haskell,monads,state-monad

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 deducing constraints works in cabal package, but not when importing from package

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...

Mixing Threepenny-Gui and StateT

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...

How can I implement a Fisher-Yates shuffle in Scala without side effects?

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._...

stacking StateT in scalaz

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...