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)
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.
Because getLine is a function, it needs arguments. There are no arguments given in this code.
2. What is the type of this function?
f name = putStrLn ("hello " ++ name)
[Char] -> ()
[Char] -> IO ()
IO [Char]
3. How do you find the type of a defined function f in ghci?
:load f
:show f
:type f
4. What is the difference between
->
and
<-
in Haskell syntax?
<-
indicates less than, whereas
->
indicates greater than
<-
is for associating names with values in do blocks whereas
->
is used for defining functions.
they are both two ways of representing the same thing.
5. Why do you think the generation and use of pseudo-random numbers might occur inside a monad?
because the sequence of pseudo-random numbers is important, and the programmer needs to control it.
because it is an interaction with ‘the outside world’
because it is defined in the Haskell Prelude library
Submit Quiz