function definitions Flashcards

1
Q

Give the polymorphic type for each of the following functions:

(a) the identity function [2]

A

(a) id :: a -> a

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

(b) the map function [2]

A

(b) map :: (a -> b) -> [a] -> [b]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

(c) the zip function [2]

A

(c) zip :: [a] -> [b] -> [(a,b)]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

(d) the foldr function [2]

A

(d) foldr :: Foldable t => (a -> b -> b) -> b -> t a ->b

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

(e) the monad bind function, i.e. (»=)

A

(e) (»=) :: Monad m => m a -> (a -> m b) -> m b

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Give function definitions for the following functions:

(a) the function length :: [a] -> Int, which operates on arbitrary lists
(i) using pattern-matching on the list structure [3]
(ii) using a foldr function, in point-free style [2]

A

(i) length :: [a] -> Int
length [] = 0
length (x:xy) = 1 + length(xy)

(ii)
length :: [a] -> Int
length = foldr (_ n -> n + 1) 0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

(b) the function sum :: [Int] -> Int, which operates on integer lists
(i) using pattern-matching on the list structure [3]
(ii) using a foldr function, in point-free style

A

(b) (i)
sum :: [Int] -> Int
sum [] = 0
sum (x:xs) = x + sum(xs)

(ii)
sum :: [Int] -> Int
sum = foldr (+) 0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly