Functional Programming (H) - 2.12 - What do you know about Haskell?
1. What is wrong with this line of code, to return the number of characters typed by a user?
let x = getLine in length(x)
Because getLine is a function, it needs arguments. There are no arguments given in this code.
nothing is wrong — the code should work fine
the code loops for ever
The code associates the name x with the getLine function, rather than receiving a line of input from the user — and we can’t take length of a function.
2. What is the type of this function?
f name = putStrLn ("hello " ++ name)
IO [Char]
[Char] -> ()
[Char] -> IO ()
3. How do you find the type of a defined function f in ghci?
:show f
:type f
:load f
4. What is the difference between
->
and
<-
in Haskell syntax?
they are both two ways of representing the same thing.
<-
is for associating names with values in do blocks whereas
->
is used for defining functions.
<-
indicates less than, whereas
->
indicates greater than
5. Why do you think the generation and use of pseudo-random numbers might occur inside a monad?
because it is defined in the Haskell Prelude library
because it is an interaction with ‘the outside world’
because the sequence of pseudo-random numbers is important, and the programmer needs to control it.
Submit Quiz