Functional Programming (H) - Examinable

1. What does the following expression evaluate to?

['a','d' .. 'z']

2. What is the result of the following expression in Haskell, where sqrt returns the square root of its argument:

sqrt 16+9

3. What is the result of evaluation of the following expression?

(\x y -> x*x-y*y) 3 4

4. What is the effect of the following fold?

foldl (\acc elt -> acc++[elt]) "" "A string"

5. In Haskell, what is another appropriate name for this style of expression:

n = n + 1

6. What is the difference between an expression and a statement in an imperative programming language

7. Given a Tree data type as defined earlier in the course:

data Tree = Leaf | Node Int Tree Tree deriving Show

with Leaf and Node constructors, then how do we define an infinite tree?

8. What is the type of this function?

f name = putStrLn ("hello " ++ name)

9. What is wrong with this line of code, to return the number of characters typed by a user?

let x = getLine in length(x)

10. A recursive function must have at least two cases. What are these called?

11. Does the following expression terminate?

let bot = bot bottomList = repeat bot in length(take 5 bottomList)

12. Which one of the following expressions should always evaluate to True?

13. What is the value of this expression?

let x = 5 in x == 5

14. How do you find the type of a defined function f in ghci?

15.
let x = y + 2 y = x/3 in x+y

This code defines a pair of simultaneous equations. Will this work in Haskell?

16. What is the result of evaluating the following expression?

map (\x -> length x) ["This","is", "a","test"]

17. Which one of the following expressions generates an infinite list of successive factorial numbers? (Recall that the nth factorial is the product of the first n positive integers.)

18. Why do you think the generation and use of pseudo-random numbers might occur inside a monad?

19. What is the type of the putStrLn function?

20. What is the difference between a named function and a lambda function assigned to a variable? For example:

f x = x + 1 -- or f = \x -> x+1

21. Study the Haskell function f below. What does f() evaluate to?

f :: () -> String f () = let x = (Just Nothing) in case x of (Just _) -> "something" Nothing -> "nothing"

22. What’s wrong with the following Haskell expression?

if (1) then "true" else "false"

23. Which one of the following expressions does not evaluate to 42?

24. Which one of the following functions will not loop infinitely, if we evaluate it in ghci?

25. Select which one of the following two let expressions will evaluate to the String

"prime minister"

26. What is the type of the following function (use a,b,c etc as type variables in order of occurence):

\x y -> y

27. What is the result of evaluating the following expression?

(\x -> (\y -> y x)) "x" (\y -> y)

28. Give one reason why the following code is incorrect in Haskell:

x = 4 x = ((x * 2))

29. What is the result of the expression abs -6 in Haskell, where abs returns the absolute value of its argument.

30. Complete the following type definition to define a binary tree with the values stored only in the leaf nodes:

data Tree a = Node __ __ | Leaf __

31. What is the result of evaluating the following expression?

(\x -> 1) 2

32. What is the wrong with the following map/fold-based computation?

foldl (+) (map (*2) [1..8])

33. What is the result of evaluating the following expression?

(\x -> (\y -> x y)) "x"

34. Given a list xs, what does the following expression evaluate to?

[]++xs == xs++[]

35. What is the missing case clause in the following definition of a function to calculate the length of a Haskell list?

mylength l = case l of -- MISSING CLAUSE -- x:xs -> 1+mylength xs

36. In a Haskell guard expression, each of the guards evaluates to a Bool value, either True or False. What is the Bool value for the otherwise case?

37. What is the result of evaluating the following expression?

(\x f -> f x) 4 (\x -> x*x)

38. What is the result of the following expression in Haskell, where sqrt returns the square root of its argument:

sqrt (16+9)

39. Given the following type declarations:

f :: T1 -> T2 g :: T2 -> T3

And given that the following expression typechecks:

v :: T1 v = h f g

What is the type of h?

40. What is the type of the following function:

\f -> f f

41. Given the definition:

sum_ratio = \x y z -> (x + y) / z

then what is the value of:

1 + 4* sum_ratio 4 2 3

42. What is the difference between -> and <- in Haskell syntax?

43. What is the result of the following computation?

foldl (/) 16 [8,4,2,1]

44. Is it valid to write n = n + 1 in Haskell

45. Given these definitions:

a = "england" b = "scotland"

then which one of the following expressions has the greatest integer value?

46. Is the following expression correctly typed?

sq :: Int -> Float sq x = x*x

47. How do we generate an infinite list of integer 1 values?

48. What is wrong with the following definition of filter?

filter :: (a -> Bool) -> [a] -> [a] filter pred [] = [] filter pred (x:xs) | pred x = x : filter pred xs | otherwise = filter pred (x:xs)

49. Is the following expression correctly typed?

join :: String -> [String] -> String join str strs = foldl (++) str strs

50. What is the result of the following computation?

foldr (/) 1 [2,4,8]

51. What is the type of the head function?

52. Does the evaluation order of Haskell sub-expressions affect the overall value of the expression?