Professional Documents
Culture Documents
Week 1-3 Nptel Haskell HRST
Week 1-3 Nptel Haskell HRST
f x y z = y == ((x+1 == 0) /= z)
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
f a 0 = a
f a b
| a >= b = f b (mod a b)
| a < b = f b a
f x y
| x <= 0 = 0
| even x = f (x `div` 2) (y + y)
| odd x = f (x `div` 2) (y + y) + y
WEEK 1 PRACTICE
1. Consider the following Haskell definition.
f x y z = x || not (not y /= z)
f3 True _ = False
f3 _ y = ...
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
[(&&), (<)]
[Bool -> Bool -> Bool]
[Bool -> Bool]
Type error
Int -> Bool
Accepted Answers:
[Bool -> Bool -> Bool]
f = g (\x -> x)
g k [] = k 100
g k (x:xs) = g ((x*) . k) xs
Accepted Answers:
(Type: Numeric) 12000
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?
myRepeat f 0 x = [x]
myRepeat f n x = f (tail (myRepeat f (n-1) x))
WEEK 3 PRACTICE
1. What is the value of the following expression?