0% found this document useful (0 votes)
14 views8 pages

12.2. Writing Functional Programs - Advanced

The document provides advanced notes on writing functional programs for AQA Computer Science A-Level, focusing on higher-order functions such as map, filter, and fold. It explains how each function operates, including examples for applying them to lists. Additionally, it emphasizes the importance of understanding the differences between fold left and fold right.

Uploaded by

macbeth guy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views8 pages

12.2. Writing Functional Programs - Advanced

The document provides advanced notes on writing functional programs for AQA Computer Science A-Level, focusing on higher-order functions such as map, filter, and fold. It explains how each function operates, including examples for applying them to lists. Additionally, it emphasizes the importance of understanding the differences between fold left and fold right.

Uploaded by

macbeth guy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

AQA Computer Science A-Level

4.12.2 Writing functional programs


Advanced Notes

www.pmt.education
Specification:

4.12.2.1 Functional language programs


Show experience of constructing simple programs in a functional
programming language.

Higher-order functions: a function is higher-order if it takes a function


as an argument or returns a function as a result, or does both.

Have experience of using the following in a functional programming


language:
• map. ​Map​ is the name of a higher-order function that applies a given
function to each element of a list, returning a list of results.
• filter.​ Filter​ is the name of a higher-order function that processes a
data structure, typically a list, in some order to produce a new data
structure containing exactly those elements of the original data structure
that match a given condition.
• reduce or fold. ​Reduce​ or ​fold​ is the name of a higher-order function
which reduces a list of values to a single value by repeatedly applying a
combining function to the list values.

www.pmt.education
Functional Programming
Resources to help you learn a functional programming language are available in the
extra resources.

Higher Order Functions


Higher order functions either take a function as an
argument​, returns a function as its result, or does both.
The three examples you need to be aware of are map,
filter and fold.

www.pmt.education
Map
The map function takes a second function and applies it to a list of elements before
returning the new list.

Map Example 1
We have the following list:

However, we want to minus 2 from each element. To do this, we can use the map
function.

Map Example 2
We have the following list:

We want each element to be squared.

Map Example 3
There is a list of single fruit items.

www.pmt.education
:
We want to change the list so each element is plural. The easiest way of doing this is
adding an “s” on the end of each word.

Filter
The filter function returns the elements of the list which adhere to the condition given. It
is a filter IN rather than a filter OUT.

Filter Example 1
We have the following list:

We only want the elements greater than 1.

Filter Example 2
We have the following list:

We only want the elements greater than 20.

www.pmt.education
Fold
By folding a list, you can reduce the list to a single value.
Folding is also known as reducing. The fold function needs an
operator (+,- etc), a starting number and a list. The type of fold
- left or right - determines how the​ recursion​ will work. You will
only be asked questions on fold left, but it is a good idea to
have an understanding of fold right.

Fold Example 1
We have the following list:

We want to total this list.

What is happening?

www.pmt.education
In this case, the result would be the same for fold right, it would just be calculated
differently.

Fold Example 2
We have the following list:

We can use the fold function to reduce this list to a single number.

What happened?

www.pmt.education
Using fold right in this case, produces a very different answer.

www.pmt.education

You might also like