do-notation

Desugaring do-notation for Monads

不打扰是莪最后的温柔 提交于 2019-11-28 05:22:39
As I'm learning Haskell I'm realizing that do notation is just syntatic sugar: a = do x <- [3..4] [1..2] return (x, 42) Translates into a = [3..4] >>= (\x -> [1..2] >>= (\_ -> return (x, 42))) I realize that I'll probably use do-notation but I'd like to understand whats going on in translation. So purely for pedagogical reasons, is there a way for ghc/ghci to give me the corresponding bind statements for a fairly complex monad written in do-notation? Edit. It turns out lambdabot on #haskell can do this: <Guest61347> @undo do x <- [3..4] ; [1..2] ; return (x, 42) <lambdabot> [3 .. 4] >>= \ x ->

Do statement under a where clause

不想你离开。 提交于 2019-11-28 02:29:36
I'm trying to convert IO [String] to [String] with <- binding; however, I need to use a do block to do that under a where statement, but Haskell complains about the indentation all the time. Here is the code: decompEventBlocks :: IO [String] -> IO [[String]] decompEventBlocks words | words' /= [] = block : (decompEventBlocks . drop $ (length block) words') | otherwise = [] where do words' <- words let block = (takeWhile (/="END") words') What is the reason for that ? And how can we use do block in a where statement ? Moreover, is there any chance that we can have some statements before the

Expressing do block using only monadic bind syntax

心不动则不痛 提交于 2019-11-27 09:45:06
As far as I know, do blocks in Haskell are just some kind of syntactic sugar for monadic bind operators. For example, one could convert main = do f <- readFile "foo.txt" print f print "Finished" to main = readFile "foo.txt" >>= print >> print "Finished" Can all do blocks be converted to bind syntax? What about, for example, this block where f is used multiple times: main = do f <- readFile "foo.txt" print $ "prefix " ++ f print $ f ++ " postfix" Assuming we are in the IO monad, it is not possible to simply execute the readFile computation twice. How can this example (if possible at all)

What indentation is required for a case statement within a let statement?

不想你离开。 提交于 2019-11-27 02:09:28
Working in haskell, found odd behavior, stripped it down to bare bones This Works a :: Bool a = case True of True -> True False -> False But when I try b :: IO Bool b = do let b' = case True of True -> True False -> False return b' I get ghci>:l test.hs [1 of 1] Compiling Main ( test.hs, interpreted ) test.hs:16:14: parse error on input ‘->’ Failed, modules loaded: none. So I try c :: IO Bool c = do let c' = case True of True -> True False -> False return c' And this works. What? Why? Why do I need an extra indent in this case? I can't find anything on this, probably because these keyword are

Desugaring do-notation for Monads

て烟熏妆下的殇ゞ 提交于 2019-11-27 00:59:29
问题 As I'm learning Haskell I'm realizing that do notation is just syntatic sugar: a = do x <- [3..4] [1..2] return (x, 42) Translates into a = [3..4] >>= (\x -> [1..2] >>= (\_ -> return (x, 42))) I realize that I'll probably use do-notation but I'd like to understand whats going on in translation. So purely for pedagogical reasons, is there a way for ghc/ghci to give me the corresponding bind statements for a fairly complex monad written in do-notation? Edit. It turns out lambdabot on #haskell

Do statement under a where clause

与世无争的帅哥 提交于 2019-11-26 23:44:07
问题 I'm trying to convert IO [String] to [String] with <- binding; however, I need to use a do block to do that under a where statement, but Haskell complains about the indentation all the time. Here is the code: decompEventBlocks :: IO [String] -> IO [[String]] decompEventBlocks words | words' /= [] = block : (decompEventBlocks . drop $ (length block) words') | otherwise = [] where do words' <- words let block = (takeWhile (/="END") words') What is the reason for that ? And how can we use do

Expressing do block using only monadic bind syntax

主宰稳场 提交于 2019-11-26 14:53:17
问题 As far as I know, do blocks in Haskell are just some kind of syntactic sugar for monadic bind operators. For example, one could convert main = do f <- readFile "foo.txt" print f print "Finished" to main = readFile "foo.txt" >>= print >> print "Finished" Can all do blocks be converted to bind syntax? What about, for example, this block where f is used multiple times: main = do f <- readFile "foo.txt" print $ "prefix " ++ f print $ f ++ " postfix" Assuming we are in the IO monad, it is not

What indentation is required for a case statement within a let statement?

最后都变了- 提交于 2019-11-26 09:58:58
问题 Working in haskell, found odd behavior, stripped it down to bare bones This Works a :: Bool a = case True of True -> True False -> False But when I try b :: IO Bool b = do let b\' = case True of True -> True False -> False return b\' I get ghci>:l test.hs [1 of 1] Compiling Main ( test.hs, interpreted ) test.hs:16:14: parse error on input ‘->’ Failed, modules loaded: none. So I try c :: IO Bool c = do let c\' = case True of True -> True False -> False return c\' And this works. What? Why? Why