You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
Function composition and partial application are two powerful techniques in functional programming that allow you to build complex functions from simpler ones. Both are key A-Level topics.
Function composition is the process of combining two or more functions to create a new function. The output of one function becomes the input of the next.
If f : B → C and g : A → B, then the composition (f . g) : A → C is defined as:
(f . g)(x) = f(g(x))
First apply g, then apply f to the result.
In Haskell, the composition operator is the dot (.).
-- Two simple functions
double x = x * 2
addOne x = x + 1
-- Compose them: first double, then add one
doubleAndAddOne = addOne . double
doubleAndAddOne 5 -- double 5 = 10, addOne 10 = 11
-- Compose in the other order: first add one, then double
addOneAndDouble = double . addOne
addOneAndDouble 5 -- addOne 5 = 6, double 6 = 12
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.