past exam papers Flashcards

1
Q

What is a higher-order function? Give an example in Haskell to explain why
higher-order functions are useful in functional programming.

A

A higher-order function is a function that takes other functions as arguments or returns a function as result.
any recurrence example.

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

What is lazy evaluation? Give an example in Haskell to explain your answer

A

only evaluating as much as you need. e.g true or false if written well

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

Give an example of a polymorphic function to explain the notion of parametric polymorphism.

A
no class constraints.
anything that uses [a] (like reverse etc)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

write a recursive ‘sorted’ algorithm without using adjPairs

A

sorted :: Ord a => [a] -> Bool
sorted[] = []
sorted (x : []) = []
sorted(x : y : xs) = x

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

write a algorithm adjPairs that takes a list and divides it into a list of adjacent pairs

A

adjPairs :: [a] -> [(a, a)]
adjPairs[] = []
adjPairs(x : []) = []
adjPairs(x : y : xs) = (x, y) : adjPairs[y ++ xs]

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

write an algorithm sorted using adjPairs

A
sorted[] = True
sorted xs = if any (== False) | then False
                    else true
                   where
                         [x < y | (x, y)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly