monads

The Haskell way to do IO Loops (without explicit recursion)?

眉间皱痕 提交于 2020-01-24 06:45:07
问题 I want to read a list of strings seperated by newlines from STDIN, until a new line is witnessed and I want an action of the type IO [String] . Here is how I would do it with recursion: myReadList :: IO String myReadList = go [] where go :: [String] -> IO [String] go l = do { inp <- getLine; if (inp == "") then return l; else go (inp:l); } However, this method of using go obscures readability and is a pattern so common that one would ideally want to abstract this out. So, this was my attempt:

Type Variable Location in Transformers

老子叫甜甜 提交于 2020-01-23 17:02:23
问题 Consider the State type - or at least a simplified version: newtype State s a = State { runState :: s -> (a, s) } Now, let's say we want to derive the StateT monad transformer. transformers defines it as follows: newtype StateT s m a = StateT { runStateT :: s -> m (a, s) } Here, the m has been placed on the right of the function arrow, but outside the tuple. However, if we didn't know the correct answer, we might instead put m somewhere else: newtype StateT s m a = StateT { runStateT :: m (s

Direct translation of Haskell monad into Scala

不羁岁月 提交于 2020-01-23 07:54:05
问题 Trying to learn how to program monads in Scala, got some troubles Given the quick code sample import Control.Monad newtype LJ a = LJ { session :: a } instance Monad LJ where return s = LJ s (>>=) m f = f ( session m ) instance Functor LJ where fmap f m = LJ . f $ session m type SimpleLJ = LJ String auth :: String -> String -> SimpleLJ auth = undefined readFeed :: String -> SimpleLJ readFeed = undefined closeFeed :: String -> SimpleLJ closeFeed = undefined proceed = auth "123" "456" >>=

Why does Tuple not have a Monad instance?

天涯浪子 提交于 2020-01-23 05:17:02
问题 One thing I noticed was that Tuple does not have a Monad instance. Tuple does however have an Applicative instance: instance Monoid a => Applicative ((,) a) Which already extremely heavily restricts what we can make the Monad instance be. Lets look at the type signature we would get for join: instance Monoid a => Monad ((,) a) join :: Monad m => m (m a) -> m a join :: Monoid a => (a, (a, b)) -> (a, b) We can also look at the Monad laws: join $ f <$> pure x == f x join $ f <$> (mempty, x) == f

IDs from State Monad in Haskell [duplicate]

倾然丶 夕夏残阳落幕 提交于 2020-01-16 19:00:48
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Creating unique labels in Haskell I've got a datatype Person and some input data from which I will create the Persons. I'd like to have each Person have its own ID (let's say integers [0..]). I could do this with recursion, but since I'm doing this in Haskell, I'd like to understand the monads. The State Monad is probably the best for this job, I suppose? The thing is, I don't really understand lots of things:

ApplicativeDo not working with sequencing

两盒软妹~` 提交于 2020-01-14 13:23:48
问题 I have this type, basically a Kleisli arrow: {-# language DeriveFunctor #-} data Plan m i o = Plan (i -> m o) deriving Functor instance (Monad m) => Applicative (Plan m i) where pure x = Plan (\_ -> pure x) Plan f <*> Plan x = Plan (\i -> f i <*> x i) Since it has an Applicative instance, I turn on ApplicativeDo and try to build a value using do-notation: {-# language ApplicativeDo #-} myplan :: Plan IO () () myplan = do pure () pure () It doesn't work: No instance for (Monad (Plan IO ()))

Unlike a Functor, a Monad can change shape?

喜夏-厌秋 提交于 2020-01-12 11:47:07
问题 I've always enjoyed the following intuitive explanation of a monad's power relative to a functor: a monad can change shape; a functor cannot. For example: length $ fmap f [1,2,3] always equals 3 . With a monad, however, length $ [1,2,3] >>= g will often not equal 3 . For example, if g is defined as: g :: (Num a) => a -> [a] g x = if x==2 then [] else [x] then [1,2,3] >>= g is equal to [1,3] . The thing that troubles me slightly, is the type signature of g . It seems impossible to define a

Functions that look pure to callers but internally use mutation

ぐ巨炮叔叔 提交于 2020-01-11 08:27:09
问题 I just got my copy of Expert F# 2.0 and came across this statement, which somewhat surprised me: For example, when necessary, you can use side effects on private data structures allocated at the start of an algorithm and then discard these data structures before returning a result; the overall result is then effectively a side-effect-free function. One example of separation from the F# library is the library's implementation of List.map, which uses mutation internally; the writes occur on an

StackOverflow in continuation monad

筅森魡賤 提交于 2020-01-10 10:42:10
问题 Using the following continuation monad: type ContinuationMonad() = member this.Bind (m, f) = fun c -> m (fun a -> f a c) member this.Return x = fun k -> k x let cont = ContinuationMonad() I fail to see why the following gives me a stack overflow: let map f xs = let rec map xs = cont { match xs with | [] -> return [] | x :: xs -> let! xs = map xs return f x :: xs } map xs id;; let q = [1..100000] |> map ((+) 1) While the following doesn't: let map f xs = let rec map xs = cont { match xs with |

StackOverflow in continuation monad

最后都变了- 提交于 2020-01-10 10:41:27
问题 Using the following continuation monad: type ContinuationMonad() = member this.Bind (m, f) = fun c -> m (fun a -> f a c) member this.Return x = fun k -> k x let cont = ContinuationMonad() I fail to see why the following gives me a stack overflow: let map f xs = let rec map xs = cont { match xs with | [] -> return [] | x :: xs -> let! xs = map xs return f x :: xs } map xs id;; let q = [1..100000] |> map ((+) 1) While the following doesn't: let map f xs = let rec map xs = cont { match xs with |