Functional programming for A Level Computer Science
Functional programming for A Level Computer Science
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
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