past exam papers Flashcards
What is a higher-order function? Give an example in Haskell to explain why
higher-order functions are useful in functional programming.
A higher-order function is a function that takes other functions as arguments or returns a function as result.
any recurrence example.
What is lazy evaluation? Give an example in Haskell to explain your answer
only evaluating as much as you need. e.g true or false if written well
Give an example of a polymorphic function to explain the notion of parametric polymorphism.
no class constraints. anything that uses [a] (like reverse etc)
write a recursive ‘sorted’ algorithm without using adjPairs
sorted :: Ord a => [a] -> Bool
sorted[] = []
sorted (x : []) = []
sorted(x : y : xs) = x
write a algorithm adjPairs that takes a list and divides it into a list of adjacent pairs
adjPairs :: [a] -> [(a, a)]
adjPairs[] = []
adjPairs(x : []) = []
adjPairs(x : y : xs) = (x, y) : adjPairs[y ++ xs]
write an algorithm sorted using adjPairs
sorted[] = True sorted xs = if any (== False) | then False else true where [x < y | (x, y)