monads

How to flatten IO (IO ())?

寵の児 提交于 2019-12-23 13:22:46
问题 I'm just learning Haskell and monad transformers and I've found myself with an IO (IO ()) that I'd like to flatten into just IO (). I'm sure that I'm doing something wrong, but can't pinpoint exactly where I'm getting lost. Here is a simplified example of what I'm trying to do. This is a convoluted way of implementing echo , but it illustrates the problem. userInput :: Monad m => ReaderT (IO String) m (IO String) userInput = ask echo :: Monad m => ReaderT (IO String) m (IO ()) echo =

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

我是研究僧i 提交于 2019-12-23 13:16:14
问题 I'm twisting my brain into knots trying to understand how to combine the State monad with Maybe . Let's start with a concrete (and intentionally trivial/unnecessary) example in which we use a State monad to find the sum of a list of numbers: import Control.Monad.State list :: [Int] list = [1,4,5,6,7,0,3,2,1] adder :: Int adder = evalState addState list addState :: State [Int] Int addState = do ms <- get case ms of [] -> return 0 (x:xs) -> put xs >> fmap (+x) addState Cool. Now let's modify it

How do I use Name as an applicative?

ぐ巨炮叔叔 提交于 2019-12-23 13:14:05
问题 scala> val a = Need(20) a: scalaz.Name[Int] = scalaz.Name$$anon$2@173f990 scala> val b = Need(3) b: scalaz.Name[Int] = scalaz.Name$$anon$2@35201f scala> for(a0 <- a; b0 <- b) yield a0 + b0 res90: scalaz.Name[Int] = scalaz.Name$$anon$2@16f7209 scala> (a |@| b) res91: scalaz.ApplicativeBuilder[scalaz.Name,Int,Int] = scalaz.ApplicativeBuilde r@11219ec scala> (a |@| b) { _ + _ } <console>:19: error: ambiguous implicit values: both method FunctorBindApply in class ApplyLow of type [Z[_]](implicit

Flatten monad stack

£可爱£侵袭症+ 提交于 2019-12-23 12:33:50
问题 So I have this sort of code all over my first serious haskell project: f :: (MonadTrans t) => ExceptT () (t (StateT A B)) C f = do mapExceptT lift $ do lift $ do ... lift $ do ... r <- ... ... return r >>= \r -> ... There definitely may be something wrong about how I try to achieve my goals (there might be simpler ways how to do it) but currently I am interested in learning how to handle a stack of monad transformers in some nicer way, if there is one. This is the only way I figured out how

Why am I getting a type error in this sequence of parsers (lecture 8 by Erik Meijer)?

Deadly 提交于 2019-12-23 12:18:43
问题 I'm in the process of watching the Functional Programming Fundamentals lecture series by Erik Meijer (with slides by Graham Hutton). In lecture 8 (on functional parsers), after defining the Parser a type, introducing a few parsing primitives (including item and return, which I named return' ), Erik gives a simple example of how his parsers can be combined in a sequence, using the do syntax: type Parser a = String -> [(a,String)] item :: Parser Char item = \inp -> case inp of [] -> [] (x:xs) -

Implementation of flatMap() for State transition

青春壹個敷衍的年華 提交于 2019-12-23 12:17:06
问题 Exercise 6.8, Chiusano and Bjarnason, Functional Programming in Scala , p. 87 asks how one might implement flatMap() for the following trait: trait RNG { def nextInt: (Int, RNG) } type Rand[+A] = RNG => (A, RNG) The answer key gives the following solution: def flatMap[A,B](f: Rand[A])(g: A => Rand[B]): Rand[B] = rng => { val (a, r1) = f(rng) g(a)(r1) // We pass the new state along } Stackoverflow provides many answers to flatMap()/monad questions, but none which for me answered my questions

MonadTransControl instance for a custom monad

最后都变了- 提交于 2019-12-23 10:38:51
问题 The docs for monad-control provide an example on how to create an instance of MonadTransControl using defaultLiftWith and defaultRestoreT . The example is for the following newtype : newtype CounterT m a = CounterT {unCounterT :: StateT Int m a} This example can be adjusted to work for any newtype that is defined using only one "elementary" monad transformer (such as the ones from transformers or mtl ). But what about the case where the stack contains two "elementary" transformers? For

State and IO Monads

六眼飞鱼酱① 提交于 2019-12-23 07:47:58
问题 I've been trying to wrap my head around the concept of monads and I've been experimenting with the following example: I have an Editor data-type that represents the state of a text document and some functions that work on it. data Editor = Editor { lines :: [Line], -- editor contents are kept line by line lineCount :: Int, -- holds length lines at all times caret :: Caret -- the current caret position -- ... some more definitions } deriving (Show) -- get the line at the given position (first

Haskell `forever` type signature

人走茶凉 提交于 2019-12-23 07:29:34
问题 In Haskell why is type-signature of forever forever :: Monad m => m a -> m b Specifically why isn't it just :: Monad m => m a -> m a ? Surely the type of monad we are acting upon doesn't change half way through forever ? A function such as: forever' :: Monad m => m a -> m a forever' = forever seems to work exactly the same. 回答1: The type signature of forever is crafted to suggest that its result runs forever. Specifically, there is no way to write an action of type m b (polymorphic in its

Haskell `forever` type signature

我的未来我决定 提交于 2019-12-23 07:29:09
问题 In Haskell why is type-signature of forever forever :: Monad m => m a -> m b Specifically why isn't it just :: Monad m => m a -> m a ? Surely the type of monad we are acting upon doesn't change half way through forever ? A function such as: forever' :: Monad m => m a -> m a forever' = forever seems to work exactly the same. 回答1: The type signature of forever is crafted to suggest that its result runs forever. Specifically, there is no way to write an action of type m b (polymorphic in its