Declarative Style of Programming: Prescribing How It Should Do It
Declarative Style of Programming: Prescribing How It Should Do It
Mathematical functions
Def: A mathematical function is a
mapping of members of one set,
called the domain set, to another set,
called the range set
A lambda expression specifies the
parameter(s) and the mapping of a
function in the following form
(x) x * x * x
for the function cube (x) = x * x * x
Mathematical functions
Lambda expressions describe
nameless functions
Lambda expressions are applied to
parameter(s) by placing the
parameter(s) after the expression
e.g. ((x) x * x * x)(3)
which evaluates to 27
Mathematical functions
Functional Forms
Def: A higher-order function, or
functional form, is one that either takes
functions as parameters or yields a
function as its result, or both
Example
A function that operates on other functions
is called a functional form. e.g.,
g(f, [x1, x2, ]) = [f(x1), f(x2), ],
becomes
g(Square, [2, 3, 5]) = [4, 9, 25]
Functions in FP languages
Given a set of input values, a function produces an output
value
Given the same input values, a function always produces
the same output value
Functions can use only the information provided by
their parameters
This excludes functions that return the date, the
time, or a random number
Functions have no side effects
Functions dont do input or output
Functions dont change the values of any variables or
data
Functions only return a value
8
10
Lists
Lists are the original persistent data structures, and are
very heavily used in functional programming
[w, x, y, z]
[x, y, z]
[y, z]
Standard higher-order
functions
A higher-order function is a function that (1) takes a function
as a parameter, or (2) returns a function as its value, or both
map(Fun, List)
Applies Fun to each element of List, returning a list of the results.
The result may contain values of a type different that those in List .
filter(Pred, List)
Returns a list of the elements of List that satisfy the predicate Pred.
The result will be a sublist of List .
14