do-notation

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

你离开我真会死。 提交于 2019-12-05 10:23:00
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 be displayed before the program terminates. Short answer : No , we have to declare a main = , but not a

A mystery involving putStrLn

最后都变了- 提交于 2019-12-02 10:13:05
问题 Why does the piece of code below produce the error parse error on input ‘putStrLn’ ? main = do line <- fmap reverse getLine putStrLn $ "You said " ++ line ++ " backwards!" putStrLn $ "Yes, you said " ++ line ++ " backwards!" <interactive>:11:4: error: parse error on input ‘putStrLn’ Also, why does the following piece of code produce the error parse error on input ‘let’ ? main = do line <- getLine let line' = reverse line putStrLn $ "You said " ++ line' ++ " backwards!" putStrLn $ "Yes, you

Is do notation used necessarily in the context of monad?

蓝咒 提交于 2019-12-02 08:20:45
问题 Haskell report 2010 says A do expression provides a more conventional syntax for monadic programming . It allows an expression such as putStr "x: " >> getLine >>= \l -> return (words l) to be written in a more traditional way as: do putStr "x: " l <- getLine return (words l) Haskell the Craft of Functional programming by Thompson says We'll continue to use the do notation, but will keep in mind that it essentially boils down to the existence of a function (>>=) which does the work of

A mystery involving putStrLn

落爺英雄遲暮 提交于 2019-12-02 07:35:11
Why does the piece of code below produce the error parse error on input ‘putStrLn’ ? main = do line <- fmap reverse getLine putStrLn $ "You said " ++ line ++ " backwards!" putStrLn $ "Yes, you said " ++ line ++ " backwards!" <interactive>:11:4: error: parse error on input ‘putStrLn’ Also, why does the following piece of code produce the error parse error on input ‘let’ ? main = do line <- getLine let line' = reverse line putStrLn $ "You said " ++ line' ++ " backwards!" putStrLn $ "Yes, you said " ++ line' ++ " backwards!" <interactive>:31:4: error: parse error on input ‘let’ Both snippets have

Is do notation used necessarily in the context of monad?

[亡魂溺海] 提交于 2019-12-02 04:21:38
Haskell report 2010 says A do expression provides a more conventional syntax for monadic programming . It allows an expression such as putStr "x: " >> getLine >>= \l -> return (words l) to be written in a more traditional way as: do putStr "x: " l <- getLine return (words l) Haskell the Craft of Functional programming by Thompson says We'll continue to use the do notation, but will keep in mind that it essentially boils down to the existence of a function (>>=) which does the work of sequencing I/O programs, and binding their results for future use. Do the above mean that do notation is used

Haskell do notation to bind

孤人 提交于 2019-12-01 16:27:24
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 =) Gabriel Gonzalez numberNode x >>= \num -> numberTree t1 >>= \nt1 -> numberTree t2 >>= \nt2 -> return (Node num nt1 nt2) Note that this is simpler if you use Applicatives: Node <$> numberNode x <*>

Why there needs to be a $ in calls like “runSomeMonad $ do …”?

房东的猫 提交于 2019-11-30 14:29:35
问题 Apparently the only possible interpretation of runSomeMonad do ... is runSomeMonad (do ...) . Why isn't the first variant allowed by the Haskell syntax? Is there some case where foo do bar could be actually ambiguous? 回答1: Note that you can observe this effect with not just do , but also let , if , \ , case , the extensions mdo and proc …and the dread unary - . I cannot think of a case in which this is ambiguous except for unary - . Here’s how the grammar is defined in the Haskell 2010

State Monad, sequences of random numbers and monadic code

走远了吗. 提交于 2019-11-30 12:03:02
问题 I'm trying to grasp the State Monad and with this purpose I wanted to write a monadic code that would generate a sequence of random numbers using a Linear Congruential Generator (probably not good, but my intention is just to learn the State Monad, not build a good RNG library). The generator is just this (I want to generate a sequence of Bool s for simplicity): type Seed = Int random :: Seed -> (Bool, Seed) random seed = let (a, c, m) = (1664525, 1013904223, 2^32) -- some params for the LCG

Why there needs to be a $ in calls like “runSomeMonad $ do …”?

心已入冬 提交于 2019-11-30 10:48:54
Apparently the only possible interpretation of runSomeMonad do ... is runSomeMonad (do ...) . Why isn't the first variant allowed by the Haskell syntax? Is there some case where foo do bar could be actually ambiguous? Jon Purdy Note that you can observe this effect with not just do , but also let , if , \ , case , the extensions mdo and proc …and the dread unary - . I cannot think of a case in which this is ambiguous except for unary - . Here’s how the grammar is defined in the Haskell 2010 Language Report, §3: Expressions . exp → infixexp :: [context =>] type | infixexp infixexp → lexp qop

State Monad, sequences of random numbers and monadic code

孤者浪人 提交于 2019-11-30 01:55:19
I'm trying to grasp the State Monad and with this purpose I wanted to write a monadic code that would generate a sequence of random numbers using a Linear Congruential Generator (probably not good, but my intention is just to learn the State Monad, not build a good RNG library). The generator is just this (I want to generate a sequence of Bool s for simplicity): type Seed = Int random :: Seed -> (Bool, Seed) random seed = let (a, c, m) = (1664525, 1013904223, 2^32) -- some params for the LCG seed' = (a*seed + c) `mod` m in (even seed', seed') -- return True/False if seed' is even/odd Don't