Modadic designations inside inside, is this possible?

Consider the following valid Haskell code

module Main where main :: IO () main = do let x = f print x f :: Maybe (Int, Int) f = Just 3 >>= (\a -> Just 5 >>= (\b -> return (a, b))) 

where the function f can be rewritten equivalent to the so-called do-notation

 f :: Maybe (Int, Int) f = do a <- Just 3 b <- Just 5 return (a, b) 

Which annoys me, the notation will not work when I put the contents of f inline. The following code does not even parse:

 main :: IO () main = do let x = do a <- Just 3 b <- Just 5 return (a, b) print x 

I correct that inside let I have to resort to (>>=) ?

While I am, the following code does not parse:

 module Main where main :: IO () main = do let x = Just 3 >>= (\a -> Just 5 >>= (\b -> return (a, b))) print x 

I see no obvious reason other than the unnecessary limited power of let . Is there an elegant way to use bind inside let ?

+5
source share
1 answer

I correct that inside let I have to resort to (>>=) ?

<Not p> No:
 main :: IO () main = do let x = do a <- Just 3 b <- Just 5 return (a, b) print x 

The Haskell layout determines that the body of the e binding in p = e mus should be at least at the beginning of p (or the first binding if you use several at the same time). Since let in do follows (almost) the same rules as let … in , you can check this with the following function:

 f :: Int f = let x = 3 + 5 in x 

This does not work, since 3 + 5 does not have the same or more indentation level as x . Nonetheless,

 f :: Int f = let x = 3 + 5 in x 

works. Also, while main is working, it doesn't actually convey that a and b are things in the x do block, so it's a little better to defer them a bit more:

 main :: IO () main = do let x = do a <- Just 3 b <- Just 5 return (a, b) print x 
+8
source

Source: https://habr.com/ru/post/1236979/


All Articles