state-monad

How can I write this simple code using the state monad?

 ̄綄美尐妖づ 提交于 2019-12-20 03:22:05
问题 I'm a beginner at Haskell and I've come across a situation where I would like to use the state monad. (Or at least, I think I that's what I'd like to use.) There are a million tutorials for the state monad, but all of them seem to assume that my main goal is to understand it on a deep conceptual level, and consequently they stop just before the part where they say how to actually develop software with it. So I'm looking for help with a simplified practical example. Below is a very simple

stacking StateT in scalaz

。_饼干妹妹 提交于 2019-12-19 13:39:05
问题 I'm trying to understand Monad Transformers in Scala by porting some examples from this tutorial by Dan Piponi: http://blog.sigfpe.com/2006/05/grok-haskell-monad-transformers.html I did a couple of easy ones: import Control.Monad.State import Control.Monad.Identity test1 = do a <- get modify (+1) b <- get return (a,b) test2 = do a <- get modify (++"1") b <- get return (a,b) go1 = evalState test1 0 go2 = evalState test2 "0" becomes: import scalaz._, Scalaz._ val test1 = for { a <- get[Int] _ <

How does this State monad code works?

荒凉一梦 提交于 2019-12-19 08:58:49
问题 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, n+1)) instance Monad ST where -- return :: a -> ST a return x = S (\s -> (x,s)) -- (>>=) :: ST a -> (a -> ST b) -> ST b st >>= f = S (\s -> let (x,s') = apply st s in apply (f x) s') data Tree a = Leaf a | Node (Tree a) (Tree a) deriving (Show) mlabel :: Tree a -> ST (Tree

ST Monad == code smell?

﹥>﹥吖頭↗ 提交于 2019-12-17 21:47:23
问题 I'm working on implementing the UCT algorithm in Haskell, which requires a fair amount of data juggling. Without getting into too much detail, it's a simulation algorithm where, at each "step," a leaf node in the search tree is selected based on some statistical properties, a new child node is constructed at that leaf, and the stats corresponding to the new leaf and all of its ancestors are updated. Given all that juggling, I'm not really sharp enough to figure out how to make the whole

Updating a Big State Fast in Haskell

你。 提交于 2019-12-14 00:15:56
问题 For my vector graphics library in Haskell I must carry around a rather big state: line stroke parameters, colors, clip path etc. I know two ways of doing this. Quoting a comment from Haskell-cafe: "I would suggest you either use a reader monad with mutable state, or a state monad with immutable state". Here is my problem: updating a big immutable state is a performance kill. Using lots of STRefs is like writing C in Haskell: it's verbose and ugly. Here is the immutable state: data GfxState =

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

天涯浪子 提交于 2019-12-13 14:22:56
问题 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 combining different monads using transformers and the like. It seems to me, that analyzing other's takes on such tasks helps the best when learning how to do it myself. The imperative code: while (true) { while (x = get()) { // Think of this as

How to preserve information when failing?

a 夏天 提交于 2019-12-13 14:19:00
问题 I'm writing some code that uses the StateT monad transformer to keep track of some stateful information (logging and more). The monad I'm passing to StateT is very simple: data CheckerError a = Bad {errorMessage :: Log} | Good a deriving (Eq, Show) instance Monad CheckerError where return x = Good x fail msg = Bad msg (Bad msg) >>= f = Bad msg (Good x) >>= f = f x type CheckerMonad a = StateT CheckerState CheckerError a It's just a Left and Right variant. What troubles me is the definition of

Haskell - Unable to define a State monad like function using a Monad like definition

我是研究僧i 提交于 2019-12-12 01:05:31
问题 I am trying to understand the concept of Monad by attempting to write generic version of functions that might be then include side effects to log, change state. Here is what I came up with: (The code is bit long, but it is there to show how I approached understanding monad - and this approach may not be correct) data Maybe' a = Nothing' | Just' a deriving Show sqrt' :: (Floating a, Ord a) => a -> Maybe' a sqrt' x = if x < 0 then Nothing' else Just' (sqrt x) inv' :: (Floating a, Ord a) => a ->

Class set method in Haskell using State-Monad

穿精又带淫゛_ 提交于 2019-12-11 18:19:27
问题 I've recently had a look at Haskell's Monad - State. I've been able to create functions that operate with this Monad, but I'm trying to encapsulate the behavior into a class, basically I'm trying to replicate in Haskell something like this: class A { public: int x; void setX(int newX) { x = newX; } void getX() { return x; } } I would be very grateful if anyone can help with this. Thanks! 回答1: I would start off by noting that Haskell, to say the least, does not encourage traditional OO-style

Haskell State as function type

混江龙づ霸主 提交于 2019-12-11 13:42:32
问题 I am having a hard time understanding this tutorial: https://acm.wustl.edu/functional/state-monad.php I am creating my own function that reverses a list and returns a State with the lowest element and the reverse of the list. I am very new to Haskell as well. Here is my code: myFunct :: Ord a => [a] -> State a [a] myFunct t = do let s = reverse t let a = minimum t return s a I can't find an other material on this either. This is the error I am getting. Couldn't match type ‘[a]’ with ‘StateT a