monads

StackOverflow in continuation monad

荒凉一梦 提交于 2020-01-10 10:41:23
问题 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 |

Is Haskell's mapM not lazy?

偶尔善良 提交于 2020-01-09 03:49:30
问题 UPDATE: Okay this question becomes potentially very straightforward. q <- mapM return [1..] Why does this never return? Does mapM not lazily deal with infinite lists? The code below hangs. However, if I replace line A by line B, it doesn't hang anymore. Alternatively, if I preceed line A by a "splitRandom $", it also doesn't hang. Q1 is: Is mapM not lazy? Otherwise, why does replacing line A with line B "fix this" code? Q2 is: Why does preceeding line A with splitRandom "solve" the problem?

Concise way to conditionally update map in State monad

夙愿已清 提交于 2020-01-07 04:19:15
问题 Below is the code from an answer regarding memoization, showing a memoization function used in the State monad, where the state is updated with the result of the passed function if the key is not already in the map. type MyMemo a b = State (Map.Map a b) b myMemo :: Ord a => (a -> MyMemo a b) -> a -> MyMemo a b myMemo f x = do map <- get case Map.lookup x map of Just y -> return y Nothing -> do y <- f x modify $ \map' -> Map.insert x y map' return y It doesn't seem like idiomatic Haskell: it

Writer monad and unsequence

你说的曾经没有我的故事 提交于 2020-01-06 18:10:14
问题 I am using the Writer monad to keep track of an error ("collision") flag on arbitrary values (such as Int ). Once the flag is set it is "sticky" and attaches itself to all values produced as a result of any operation with the marked one. Sometimes the collision flag is associated with individual values, sometimes I would like to associate with composite structures such as lists. Of course, once the collision flag is set for a whole list, it also makes sense to assume it is set for an

Monad and MonadIO for custom type

♀尐吖头ヾ 提交于 2020-01-06 14:16:05
问题 I have a Logger type of kind * -> * which can take any type and log the value in a file. I am trying to implement this in a monadic way so that I log and keep working the same. My code looks like import Control.Applicative import Control.Monad import System.IO import Control.Monad.IO.Class instance Functor Logger where fmap = liftM instance Applicative Logger where pure = return (<*>) = ap newtype Logger a = Logger a deriving (Show) instance Monad (Logger) where return = Logger Logger logStr

Extracting nested monadic result: m (m a) -> m a

百般思念 提交于 2020-01-06 01:45:20
问题 I have a function parseArgs :: [String] -> StdGen -> IO () which selects the function to run. The main looks like main = parseArgs <$> getArgs <*> getStdGen >>= id The problem I have, parseArgs <$> getArgs <*> getStdGen is of type IO (IO ()) , which I extract using (>>= id) which is of type Monad m => m (m b) -> m b . Is there a way to avoid requiring the "extraction" of the value while having just a single line function? 回答1: The easiest way would be with join : main = join $ parseArgs <$>

Execution monad

感情迁移 提交于 2020-01-05 09:10:34
问题 I know monad is the general concept. What about Execution monad. Is it a general concept or design Patten which can be used outside scalding too. I have seen new version of scalding is having execution monads. 回答1: It is just one specific monad, which is part of scalding. So, not a general concept. You could use similar monads in other contexts outside of scalding, but the exact monad would be different, and the term "execution monad" doesn't seem to be commonly used except to refer

Trouble using the getTime function

若如初见. 提交于 2020-01-04 14:18:13
问题 I am using getTime inside an assert statement on a contract choice as follows: Add_Car : CarId with startCoverage: Time do -- Check for a legal start date assert ( startCoverage > getTime ) create this with datetime_vehicle_added = startCoverage, covered=True It generates an error: error: * Couldn't match expected type `Time' with actual type `m0 Time' * In the second argument of `(>)', namely `getTime' In the first argument of `assert', namely `(startCoverage > getTime)' In a stmt of a 'do'

state monad haskell

痴心易碎 提交于 2020-01-04 06:50:24
问题 I want to write a function for calculating the average using the State Monad in haskell this is the code I wrote as far import Control.Monad.State type MyState = (Double,Double) media s (a,n)= ((a*n+s)/(n+1),n+1) getAverage:: Double ->State MyState s1-> Double getAverage s c=get >>= \s0 -> let (x,s1) =media s s0 in put s1 >> return x I got this error when compile in GHCI, and I stuck there can you help me to understand what is wrong, thank you in advance 回答1: The code you provided gives this

Pattern matching against monadic result?

点点圈 提交于 2020-01-04 05:12:11
问题 I am learning Haskell and want to use "readHex", which according to Hoogle has type: readHex :: Num a => ReadS a How do you "extract" a result from such a function? What's the most common way, pattern match against the right constructor ie, [(a,"")] ?? LiftM and lifting in general seems to make some sense, but I'm lost when it comes to "unwinding" the monadic stack. 回答1: To answer the general question in general terms, the only way to extract values from a data constructor is pattern matching