0% found this document useful (0 votes)
3 views

Functional programming for A Level Computer Science

The document provides an overview of functional programming concepts, including first-class objects, function application, partial function application, and function composition. It explains the significance of higher-order functions such as map, filter, and reduce, as well as list operations in functional programming. Additionally, it includes resources for further learning about functional programming in languages like Haskell.

Uploaded by

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

Functional programming for A Level Computer Science

The document provides an overview of functional programming concepts, including first-class objects, function application, partial function application, and function composition. It explains the significance of higher-order functions such as map, filter, and reduce, as well as list operations in functional programming. Additionally, it includes resources for further learning about functional programming in languages like Haskell.

Uploaded by

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

Functional

Programming
Functional Programming
First Class Object
Know that a function, f, has a function Loosely speaking, a function is a rule that,
type for each element in some set A of inputs,
f: A → B (where the type is A → B, A is the assigns an output chosen from set B, but
argument type, and B is the result type). without necessarily using every member
Know that A is called the domain and B is of B. For example,
called the co-domain. f: {a,b,c,…z} → {0,1,2,…,25} could use
Know that the domain and co-domain are the rule that maps a to 0, b to 1, and so
always subsets of objects in some data on, using all values which are members of
type. set B.
The domain is a set from which the
function’s input values are chosen.
The co-domain is a set from which the
function’s output values are chosen. Not
all of the co-domain’s members need to
be outputs.
Functional Programming
First Class Object
Functional Programming
First Class Object
Know that a function is a first- First-class objects (or values) are
class object in functional objects which may:appear in
programming languages and in expressions
imperative programming •be assigned to a variable
languages that support such •be assigned as arguments
objects. This means that it can be •be returned in function calls.
an argument to another function For example, integers, floating-
as well as the result of a function point values, characters and
call. strings are first class objects in
many programming languages.
Functional Programming
Function Application
Know that function application The process of giving particular
means a function applied to its inputs to a function is
arguments. called function application, for
example add(3,4) represents the
application of the function add to
integer arguments 3 and 4.
The type of the function is
f: integer x integer → integer
where integer x integer is the
Cartesian product of the
set integer with itself.
Although we would say that
function f takes two arguments, in
fact it takes only one argument,
Functional Programming
Partial Function Application
Know what is meant by The function add takes two integers as
partial function arguments and gives an integer as a result.
application for one, two Viewed as follows in the partial function
and three argument application scheme:
functions and be able to add: integer → (integer → integer)
use the notations shown add 4 returns a function which when applied
opposite. to another integer adds 4 to that integer.
The brackets may be dropped so
function add becomes add:
integer → integer → integer
The function add is now viewed as taking
one argument after another and returning a
result of data type integer.
Functional Programming
Composition of functions
Know what is The operation functional composition combines two
meant by functions to get a new function.
composition of Given two functions
functions. f: A → B
g: B → C
function g ○ f, called the composition of g and f, is a
function whose domain is A and co-domain is C.
If the domain and co-domains of f and g are ℝ, and f(x)
= (x + 2) and g(y) = y3. Then
g ○ f = (x + 2)3
f is applied first and then g is applied to the result
returned by f.
Functional Programming
Functional Language Programs
Higher-order A function is higher-order if it takes a function as an
functions. argument or returns a function as a result, or does both

Have map is the name of a higher-order function that applies


experience of a given function to each element of a list, returning a
using the list of results.
following in a filter is the name of a higher-order function that
functional processes a data structure, typically a list, in some
programming order to produce a new data structure containing
language: exactly those elements of the original data structure
•map that match a given condition.
•filter reduce or fold is the name of a higher-order function
•reduce or which reduces a list of values to a single value by
fold. repeatedly applying a combining function to the list
Functional Programming
Functional Language Programs
Functional Programming
Functional Language Programs
Functional Programming
Functional Language Programs
Functional Programming
Functional Language Programs
Functional Programming
Functional Language Programs
Functional Programming
Lists in functional programming
Be familiar with representing a list as a concatenation of a For example, in Haskell
head and a tail. the list [4, 3, 5] can be
Know that the head is an element of a list and the tail is a written in the
list. form head:tail where hea
Know that a list can be empty. d is the first item in the
Describe and apply the following operations: list and tail is the
•return head of list remainder of the list. In
•return tail of list the example, we have 4:
•test for empty list [3, 5]. We call 4
•return length of list the head of the list and
•construct an empty list [3, 5] the tail.
•prepend an item to a list [] is the empty list.
•append an item to a list.
Have experience writing programs for the list operations
mentioned above in a functional programming language or
in a language with support for the functional paradigm.
Functional Programming
Lists in functional programming
Functional Programming
Lists in functional programming
Functional Programming
Resources

https://isaaccomputerscience.or
g/concepts/prog_func_intro_to_h
askell?topic=functional_program
ming
https://www.trccompsci.online/
mediawiki/index.php/Fundamen
tals_of_functional_programmin
g

You might also like