monads

Cannot find function similar to liftM2

旧城冷巷雨未停 提交于 2019-11-29 14:19:16
myLiftM2 :: Monad m => (a -> a1 -> m b) -> m a -> m a1 -> m b myLiftM2 f x y = x >>= (\r1 -> y >>= (\r2 -> f r1 r2)) In liftM2 f return b, but myLiftM2 return m b tl;dr: Use join :: Monad m => m (m a) -> m a since a plain lift will return m (m a) . E.g. write join $ liftM2 f a b But also... liftM s can also be written with Applicative -- e.g. liftM2 a b c == a <$> b <*> c liftM3 a b c d == a <$> b <*> c <*> d etc. In this case, if you're willing to write in that style, you can write it cleanly and easily: import Control.Applicative myLiftM2 :: (Monad m, Applicative m) => (a -> a1 -> m b) -> m

What is the default type evaluation of MonadPlus in Haskell?

我们两清 提交于 2019-11-29 14:08:27
I have the following code: import Control.Monad coin :: MonadPlus m => m Int coin = return 0 `mplus` return 1 If I evaluate coin :: Maybe Int on the interpreter, it prits Just 0 . That's normal because of the implementation of Maybe as instance of MonadPlus. If I evaluate coin :: [Int] on the interpreter, it prints [0, 1] , because the implementation of mplus on list is an append . But if I evaluate coin , without any type decorators, it prints 0 . Why? What type does the interpreter 'converts' coin to evaluate it? This code is extracted from: http://homes.sice.indiana.edu/ccshan/rational

Simulating interacting stateful objects in Haskell

淺唱寂寞╮ 提交于 2019-11-29 13:29:35
I'm currently writing a Haskell program that involves simulating an abstract machine, which has internal state, takes input and gives output. I know how to implement this using the state monad, which results in much cleaner and more manageable code. My problem is that I don't know how to pull the same trick when I have two (or more) stateful objects interacting with one another. Below I give a highly simplified version of the problem and sketch out what I have so far. For the sake of this question, let's assume a machine's internal state consists only of a single integer register, so that its

Why use such a peculiar function type in monads?

我怕爱的太早我们不能终老 提交于 2019-11-29 13:26:12
New to Haskell, and am trying to figure out this Monad thing. The monadic bind operator -- >>= -- has a very peculiar type signature: (>>=) :: Monad m => m a -> (a -> m b) -> m b To simplify, let's substitute Maybe for m : (>>=) :: Maybe a -> (a -> Maybe b) -> Maybe b However, note that the definition could have been written in three different ways: (>>=) :: Maybe a -> (Maybe a -> Maybe b) -> Maybe b (>>=) :: Maybe a -> ( a -> Maybe b) -> Maybe b (>>=) :: Maybe a -> ( a -> b) -> Maybe b Of the three the one in the centre is the most asymmetric. However, I understand that the first one is kinda

How to get ReaderT to work with another monad transformer?

微笑、不失礼 提交于 2019-11-29 12:19:55
I would like to embed ReaderT into another monad transformer. How do I do this? The example below uses Scotty but I think it would be the same with any other monad. {-# LANGUAGE OverloadedStrings #-} import qualified Web.Scotty import Web.Scotty.Trans import Data.Text.Lazy import Control.Monad.IO.Class (liftIO) import Control.Monad.Trans.Reader import Control.Monad.Trans data Config = Config Text main :: IO () main = do let config = Config "Hello World" -- how to I make this line work? scottyT 3000 id id routes routes :: ScottyT Text (ReaderT Config IO) () routes = do get "/" info info ::

Difference in capability between fmap and bind?

佐手、 提交于 2019-11-29 12:18:11
问题 I'm new to functional programming (coming from javascript), and I'm having a hard time telling the difference between the two, which is also messing with my understand of functors vs. monads. Functor: class Functor f where fmap :: (a -> b) -> f a -> f b Monad (simplified): class Monad m where (>>=) :: m a -> (a -> m b) -> m b fmap takes a function and a functor, and returns a functor. >>= takes a function and a monad, and returns a monad. The difference between the two is in the function

combining StateT with InputT

穿精又带淫゛_ 提交于 2019-11-29 11:56:07
It is a follow-up to this question . I'm trying to combine shell from @ErikR's answer in my InputT loop. main :: IO [String] main = do c <- makeCounter execStateT (repl c) [] repl :: Counter -> StateT [String] IO () repl c = lift $ runInputT defaultSettings loop where loop = do minput <- getLineIO $ in_ps1 $ c case minput of Nothing -> lift $ outputStrLn "Goodbye." Just input -> (liftIO $ process c input) >> loop getLineIO :: (MonadException m) => IO String -> InputT m (Maybe String) getLineIO ios = do s <- liftIO ios getInputLine s And getting an error Main.hs:59:10: Couldn't match type

Try monad in Java 8

让人想犯罪 __ 提交于 2019-11-29 11:44:47
问题 Is there a built-in support for monad that deals with exception handling? Something similar to Scala's Try. I am asking because I don't like unchecked exceptions. 回答1: The "better-java-monads" project on GitHub has a Try monad for Java 8 here. 回答2: There are at least two generally available (e.g. on Maven Central) - Vavr and Cyclops both have Try implementations that take a slightly differing approach. Vavr's Try follows Scala's Try very closely. It will catch all 'non-fatal' exceptions

Why the Haskell sequence function can't be lazy or why recursive monadic functions can't be lazy

旧时模样 提交于 2019-11-29 09:26:43
With the question Listing all the contents of a directory by breadth-first order results in low efficiency I learned that the low efficiency is due to a strange behavior of the recursive monad functions. Try sequence $ map return [1..]::[[Int]] sequence $ map return [1..]::Maybe [Int] and ghci will fall into an endless calculation. If we rewrite the sequence function in a more readable form like follows: sequence' [] = return [] sequence' (m:ms) = do {x<-m; xs<-sequence' ms; return (x:xs)} and try: sequence' $ map return [1..]::[[Int]] sequence' $ map return [1..]::Maybe [Int] we get the same

Using Eithers with Scala “for” syntax

有些话、适合烂在心里 提交于 2019-11-29 09:02:52
As I understand it, Scala "for" syntax is extremely similar to Haskell's monadic "do" syntax. In Scala, "for" syntax is often used for List s and Option s. I'd like to use it with Either s, but the necessary methods are not present in the default imports. for { foo <- Right(1) bar <- Left("nope") } yield (foo + bar) // expected result: Left("nope") // instead I get "error: value flatMap is not a member..." Is this functionality available through some import? There is a slight hitch: for { foo <- Right(1) if foo > 3 } yield foo // expected result: Left(???) For a List, it would be List() . For