do-notation

Difference between where bindings, let bindings and the single assignment operator (<-)

江枫思渺然 提交于 2020-05-24 21:22:27
问题 I do not understand the difference between the three syntaxes: where a = f (b) do a <- f (b) do let a = f (b) I do understand somehow though that a <- f(b) is different from the other two, in most cases where I tried all three worked. Also I read somewhere on the net that per block you should try to get along with one let binding only in order to be "idiomatic". But I never seem to manage. How do I decide what to use? 回答1: let foo = bar in ... simply defines foo to be the exact same thing as

How to use variable from do block assignment line in a where clause?

孤人 提交于 2020-05-23 13:01:40
问题 I have the following sample of code: {-# LANGUAGE ScopedTypeVariables #-} main = do putStrLn "Please input a number a: " a :: Int <- readLn print a putStrLn "Please input a number b: " b :: Int <- readLn print b putStrLn ("a+b+b^2:" ++ (show $ a+b+c)) where c = b^2 For some reason I cannot use variable b in a where clause, the error I get is the following: Main3.hs:13:15: error: Variable not in scope: b | 13 | where c = b^2 | ^ Any ideas how to make b available in the where clause? 回答1: Use

Understanding do notation for simple Reader monad: a <- (*2), b <- (+10), return (a+b)

孤者浪人 提交于 2020-02-24 07:56:05
问题 instance Monad ((->) r) where return x = \_ -> x h >>= f = \w -> f (h w) w import Control.Monad.Instances addStuff :: Int -> Int addStuff = do a <- (*2) b <- (+10) return (a+b) I'm trying to understand this monad by unwiding the do notation, because I think the do notation hides what happens. If I understood correctly, this is what happens: (*2) >>= (\a -> (+10) >>= (\b -> return (a+b))) Now, if we take the rule for >>= , we must understand (*2) as h and (\a -> (+10) >>= (\b -> return (a+b)))

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 ()))

fmap into a do block fails with a print error

大城市里の小女人 提交于 2019-12-24 07:19:24
问题 I'm trying to understand why a function I have written with a do-block can't be rewritten to fmap a similar lambda expression over a list. I have the following: -- This works test1 x = do let m = T.pack $ show x T.putStrLn m test1 1 Produces 1 But -- This fails fmap (\x -> do let m = T.pack $ show x T.putStrLn m ) [1..10] -- And this also fails fmap (\x -> do T.putStrLn $ T.pack $ show x ) [1..10] With error: <interactive>:1:1: error: • No instance for (Show (IO ())) arising from a use of

Haskell do notation to bind

旧巷老猫 提交于 2019-12-19 13:46:50
问题 I´am trying to desugar a do statement in Haskell. I have found some examples here on SO but wasn´t able to apply them to my case. Only thing I can think of is a heavy nested let statement, which seems quite ugly. Statement in which do notation should be replaced by bind: do num <- numberNode x nt1 <- numberTree t1 nt2 <- numberTree t2 return (Node num nt1 nt2) Any input is highly appreciated =) 回答1: numberNode x >>= \num -> numberTree t1 >>= \nt1 -> numberTree t2 >>= \nt2 -> return (Node num

'do' construct in Haskell

杀马特。学长 韩版系。学妹 提交于 2019-12-11 06:20:54
问题 I'm trying to learn Haskell and want to write a small program which prints the content of a file to the screen. When I load it into GHCi I get the following error: The last statement in a 'do' construct must be an expression I know this question has be asked already here: Haskell — “The last statement in a 'do' construct must be an expression”. Even though my code is very similar I still can't figure out the problem. If anyone could point out the problem to me I'd be very thankful. module

How to use Control.Monad.Cont in a recursive function?

天涯浪子 提交于 2019-12-11 05:34:43
问题 I was providing an answer to this question and an idea came to me to use Cont monad. I don't know Haskell enough to explain why this program doesn't work import Control.Monad.Cont fib1 n = runCont (slow n) id where slow 0 = return 0 slow 1 = return 1 slow n = do a <- slow (n - 1) b <- slow (n - 2) return a + b main = do putStrLn $ show $ fib1 10 Error - main.hs:10:18: error: • Occurs check: cannot construct the infinite type: a2 ~ m a2 • In the second argument of ‘(+)’, namely ‘b’ In a stmt

Execution order with (>>=) not what I expected

风格不统一 提交于 2019-12-08 16:11:57
问题 I've got a series of network requests, that each take >10 seconds. So that the user knows what's happening, I give updates: main = do putStr "Downloading the first thing... " {- Net request -} putStrLn "DONE" putStr "Downloading the second thing... " {- Net request -} putStrLn "DONE" With GHCi this works as expected, but compiled or with runghc, "Downloading" doesn't print till "DONE" does. I've rewritten it with (>>=) and (>>), but I get the same problem. What's going on? 回答1: The problem

Does the main-function in Haskell always start with main = do?

北城余情 提交于 2019-12-07 06:42:32
问题 In java we always write: public static void main(String[] args){...} when we want to start writing a program. My question is, is it the same for Haskell, IE: can I always be sure to declare: main = do, when I want to write code for a program in Haskell? for example: main = do putStrLn "What's your name?" name <- getLine putStrLn ("Hello " ++ name) This program is going to ask the user "What's your name?" the user input will then be stored inside of the name-variable, and "Hello" ++ name will