You are on page 1of 6

WEEK 1

1. Consider the following Haskell definition.

f x y z = y == ((x+1 == 0) /= z)

Which of the following is a possible type of f?


(Int, Int, Int) -> Bool
Integer -> Bool -> Bool -> Bool
Int -> Int -> Bool -> Bool
Int -> Int -> Int -> Bool
Accepted Answers:
Integer -> Bool -> Bool -> Bool

2. Consider the following Haskell definition.

f x y z = (x >= y) == not (0 <= z && z <= y)

For how many pairs (x,y) does f x y 3 return False, where -2 <=
x <= 2 and -3 <= y <= 3?
10
15
25
35
Accepted Answers:
10

3. Consider the following Haskell function f.

f a 0 = a
f a b
| a >= b = f b (mod a b)
| a < b = f b a

What is the value of f 21 35?


Accepted Answers:
(Type: Numeric) 7

4. Consider the following Haskell function f.

f x y
| x <= 0 = 0
| even x = f (x `div` 2) (y + y)
| odd x = f (x `div` 2) (y + y) + y

What is the value of f 31 25?


Accepted Answers:
(Type: Numeric) 775
5. Consider the following Haskell definition.

f x y z = (not x || y) && (not y || z) && (z || x)


For how many triples (x,y,z) does f x y z evaluate to True?
Accepted Answers:
(Type: Numeric) 3

WEEK 1 PRACTICE
1. Consider the following Haskell definition.

f x y z = x || not (not y /= z)

Which of the following is a possible type of f?


(Bool, Bool, Bool) -> Bool
Bool -> Bool -> Int -> Bool
Bool -> Int -> Int -> Bool
Bool -> Bool -> Bool -> Bool
Accepted Answers:
Bool -> Bool -> Bool -> Bool
2. Consider the following incomplete definition of the NOR
function (NOR x y = True if and only if both x and y are
False).

f3 True _ = False
f3 _ y = ...

Which of the following is a correct replacement for the


... ?
False
not y
True
y
Accepted Answers:
not y
3. Consider the following recursive definition.

f n = g n 0

g n a
| n == 0 = a
| otherwise = g q (10*a + r)
where
q = div n 10
r = mod n 10

What is the value of f 5578?


Accepted Answers:
(Type: Numeric) 8755
WEEK 2

1. What is the type of the following list?

[(&&), (<)]
[Bool -> Bool -> Bool]
[Bool -> Bool]
Type error
Int -> Bool
Accepted Answers:
[Bool -> Bool -> Bool]

2. Suppose l1 and l2 are defined as follows:

l1 = filter isUpper ['a'..'z']


l2 = zipWith (<) [0..26] [1..]

What is the value of l1 == l2?


True
False
Type error
[True]
Accepted Answers:
Type error

3. What is the value of the expression f [1..5] where f is


defined as follows?

f = g (\x -> x)
g k [] = k 100
g k (x:xs) = g ((x*) . k) xs
Accepted Answers:
(Type: Numeric) 12000

4. What is the value of the expression f 6 where f is defined as


below?

f = snd . g
g 0 = (0,0)
g n = let (x,y) = g (n-1)
in (x+1, y-x)
Accepted Answers:
(Type: Numeric) -15
5. Given below is an incomplete definition for the function
drop. Complete the code by filling in the blanks.

drop n l
| n <= 0 || null l = l
| otherwise = ______

tail l
drop (n-1) (tail l)
drop (n-1) l
drop n (tail l)
Accepted Answers:
drop (n-1) (tail l)
WEEK 2 PRACTICE
1. Suppose l1 and l2 are defined as follows:
l1 = filter isUpper ['a'..'z']
l2 = [x | x <- [0,2..10], odd x].
What is the value of l1 == l2?
True
False
Type error
1
Accepted Answers:
Type error
2. What is the value of the function f defined below?
f = (l, length l)
where l = [32, 28.3..1]
Accepted Answers:
(Type: String) ([32,28.3,24.6,20.9,17.2,13.5,9.8,6.1,2.4],9)
3. Given below is an incomplete definition for the function
take. Complete the code by filling in the blanks.

take n l
| n <= 0 || null l = [ ]
| otherwise = ______
Accepted Answers:
(Type: String) head l:take (n-1) (tail l)
WEEK 3
1. What is the value of the following expression?

drop 10 (takeWhile (>30) [0,4..90])


Accepted Answers:
(Type: String) []

2. What is the value of the following expression?

take 5 (dropWhile (<30) [0,8..50])


Accepted Answers:
(Type: String) [32,40,48]

3. What is the type of the following function f?

f x y z = map (not . y) (zipWith (&&) x z)

Your answer should have no intervening space.


Accepted Answers:
(Type: String) [Bool]->(Bool->Bool)->[Bool]->[Bool]

4. What is the value of the following expression?

take 5 (foldl (\y x -> [x]:y) [] [0..99])


Accepted Answers:
(Type: String) [[99],[98],[97],[96],[95]]

5. What is the value of the following expression?

foldl (\l x -> l ++ [x + last l]) [0] [1..5]


Accepted Answers:
(Type: String) [0,1,3,6,10,15]

6. What is the position of (4,4) in the following infinite list?

[(i,k-i) | k <- [0,2..], i <- [0..k]]

Remember that list positions start with 0.


Accepted Answers:
(Type: Numeric) 20

7. Where does (2,3) occur in the following infinite list?

[(i,k-i) | k <- [0,2..], i <- [0..k]]


Does not occur
Position 10
Position 18
Position 5
Accepted Answers:
Does not occur
8. Consider the following definition of the function myRepeat.

myRepeat f 0 x = [x]
myRepeat f n x = f (tail (myRepeat f (n-1) x))

What is the most general type for the argument f?


a -> a
a -> [a]
[a] -> [a]
[a] -> a
Accepted Answers:
[a] -> [a]

WEEK 3 PRACTICE
1. What is the value of the following expression?

drop 20 (filter (>50) [0,3..90])


Accepted Answers:
(Type: String) []

2. What is the type of the following function?


f x y z = y:(x++z)
f :: [a] -> [a] -> [a]
f :: [a] -> [a] -> [a] -> [a]
f :: [a] -> a -> [a] -> [a]
f :: a -> [a] -> [a] -> [a]
Accepted Answers:
f :: [a] -> a -> [a] -> [a]

3. What is the value of the following expression?

length (foldr f [0] [0..10])


where f x y = if (x `mod` 2 == 0)
then x: x + head y : y
else x: y
Accepted Answers:
(Type: Numeric) 18

4. What is the value of the following expression?

take 5 (filter (<30) [0,8..100])


Accepted Answers:
(Type: String) [0,8,16,24]

You might also like