You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
This lesson consolidates all the functional programming concepts covered in this course through exam-style questions, model answers, and revision strategies.
Explain the difference between imperative programming and functional programming. Give one advantage of each paradigm. [6 marks]
Imperative programming describes computation as a sequence of statements that change program state. Variables are mutable and can be reassigned. Control flow uses loops (for, while) and conditionals. Programs describe how to compute a result step by step.
Functional programming describes computation as the evaluation of mathematical functions. Data is immutable and cannot be changed once created. Control flow uses recursion and function composition. Programs describe what to compute rather than how.
Advantage of imperative: More intuitive for problems involving sequential operations and state changes, such as controlling hardware or managing user interfaces.
Advantage of functional: Easier to reason about, test, and parallelise because pure functions have no side effects and data is immutable.
Explain what is meant by "first-class functions" and "higher-order functions." Give an example of each in Haskell. [6 marks]
First-class functions means that functions are treated as values — they can be assigned to variables, passed as arguments, returned from other functions, and stored in data structures.
Example:
double = (*2)
-- Here, the function (*2) is assigned to the variable 'double'
Higher-order functions are functions that take one or more functions as arguments, or return a function as their result.
Example:
applyTwice f x = f (f x)
applyTwice (+3) 10 -- Result: 16
-- applyTwice takes a function f as an argument
A language must support first-class functions for higher-order functions to be possible.
Given the list [3, 7, 2, 8, 1, 5, 9, 4], use Haskell to: (a) Double every element using map. [2 marks] (b) Keep only elements greater than 4 using filter. [2 marks] (c) Find the sum of all elements using foldl. [2 marks] (d) Combine all three to find the sum of the doubled values of elements greater than 4. [4 marks]
-- (a) Map
map (*2) [3, 7, 2, 8, 1, 5, 9, 4]
-- Result: [6, 14, 4, 16, 2, 10, 18, 8]
-- (b) Filter
filter (>4) [3, 7, 2, 8, 1, 5, 9, 4]
-- Result: [7, 8, 5, 9]
-- (c) Fold
foldl (+) 0 [3, 7, 2, 8, 1, 5, 9, 4]
-- Result: 39
-- (d) Combined
foldl (+) 0 (map (*2) (filter (>4) [3, 7, 2, 8, 1, 5, 9, 4]))
-- Step 1: filter (>4) -> [7, 8, 5, 9]
-- Step 2: map (*2) -> [14, 16, 10, 18]
-- Step 3: foldl (+) 0 -> 58
-- Or using composition:
(foldl (+) 0 . map (*2) . filter (>4)) [3, 7, 2, 8, 1, 5, 9, 4]
-- Result: 58
Write a recursive function in Haskell that takes a list of integers and returns the number of even numbers in the list. Trace through the execution for the input [3, 4, 7, 2]. [8 marks]
countEvens :: [Int] -> Int
countEvens [] = 0
countEvens (x:xs)
| even x = 1 + countEvens xs
| otherwise = countEvens xs
Trace:
countEvens [3, 4, 7, 2]
3 is odd -> countEvens [4, 7, 2]
4 is even -> 1 + countEvens [7, 2]
7 is odd -> countEvens [2]
2 is even -> 1 + countEvens []
[] -> 0
= 1 + (1 + 0)
= 2
The function correctly returns 2, as there are two even numbers (4 and 2) in the list.
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.