Post on 05-Apr-2018
7/31/2019 SSG314 Lecture Two Functional Programming
1/24
Functional ProgrammingMathematica as a Functional Programming Language
7/31/2019 SSG314 Lecture Two Functional Programming
2/24
A OOP language has methods, operators and
properties. These are functions. The programs in this paradigm evolves by changes of
the state of objects which are acted upon by theprograms.
Functions in an OOP environment often are used tocreate side effects. Functional Programming takes adifferent approach. The symbolics processor inMathematica supports this way of doing things.
Object-Oriented ProgrammingOOP
7/31/2019 SSG314 Lecture Two Functional Programming
3/24
Functional programming is a style of programming that
emphasizes the evaluation of expressions rather than theexecution of commands. Erlang programming language is
described as a functional programming language. Erlang
avoids the use of global variables that can be used in
common by multiple functions since changing such avariable in part of a program may have unexpected effects
in another part.
What is Functional Programming?
http://whatis.techtarget.com/definition/0,,sid9_gci212072,00.htmlhttp://whatis.techtarget.com/definition/0,,sid9_gci212072,00.htmlhttp://whatis.techtarget.com/definition/0,,sid9_gci212072,00.htmlhttp://whatis.techtarget.com/definition/0,,sid9_gci212072,00.html7/31/2019 SSG314 Lecture Two Functional Programming
4/24
In an earlier definition from the ITU-TS, functionalprogramming is "a method for structuring programsmainly as sequences of possibly nested functionprocedure calls." A function procedure is a relatively
simple program that is called by other programs andderives and returns a value to the program that called it
What is Functional Programming?
7/31/2019 SSG314 Lecture Two Functional Programming
5/24
In this section we shall examine the facilities in
Mathematica that support the functional approach toprogramming.
Now a function is a map with a domain and a range.Mathematically, a function has a name, parameters
and a body that is basically a blueprint for creatingthe range out of the domain parameters.
A pure function has no name.
Support for Functional Programming
7/31/2019 SSG314 Lecture Two Functional Programming
6/24
It may come as a shock to see that a function doesnot necessarily need a name. If the way to generatethe range from the domain can be specified without aname, we can still have a function. Below arenameless functions with one and two arguments:
Lambda functions
In these examples, we have
specified maps and the way thatthese maps are constructedwithout the superfluity of aname! These are PureFunctions NamelessFunctions
7/31/2019 SSG314 Lecture Two Functional Programming
7/24
We can attach a name to a pure function as follows:
The concluding ampersand &signifies the end of the functionbody.
While a function can do without
a name, it cannot be made toproduce a map without its beingendued with a body. The parameters can be specified andnumbered when they are more than one as shown
Attaching a name
7/31/2019 SSG314 Lecture Two Functional Programming
8/24
Mathematica calls nameless functions Purefunctions
In this course we shall avoid that nomenclaturebecause we already call functions that have no bodiesin OOP Pure functions. Nameless functions, as wehave seen have bodies. We adopt the commonlanguage agnostic concept of Lambda function.
A Lambda Function can be spacified, as we havealready done without the need for a name.
Attaching a name
7/31/2019 SSG314 Lecture Two Functional Programming
9/24
There are four language constructs that are so basic
and will need to master to successfully operate in afunctional environment:
Apply, Map, Fold and Nest.
We will take these in order with simple examples. To
succeed in this course, it will be important to practicewith your own chosen examples and to experimentuntil you have mastered these concepts.
Function Primitives
7/31/2019 SSG314 Lecture Two Functional Programming
10/24
Apply resets the Head of anexpression to the new headspecified in the firs argument ofApply.
The Head was changed form Listto f as we can see.
When we change the Head toPlus, the summation wasautomatically applied to the entirelist.
Apply
7/31/2019 SSG314 Lecture Two Functional Programming
11/24
There an operator form of Apply as
can be seen in the example here. It works the same way as the
previous page.
Apply can also be used in the infix
way as we could have done for anyother operator:
Apply in Operator form
7/31/2019 SSG314 Lecture Two Functional Programming
12/24
Map is a repeated application [Apply used repeatedly]
that can be done in levels. Map can be expressed in the functional or operator
form as follows:
The level of the map in the last example is 2.
Map
7/31/2019 SSG314 Lecture Two Functional Programming
13/24
As we saw in apply, there is a Map operator. /@. It is
possible to use both the operator as well as the Infixform to Mapping.
The other two functions: Fold and Nest are based onmapping and applying as we shall see. It is important
to understand the latter in order to fully grasp the fullmeaning of the former.
Map Operator
7/31/2019 SSG314 Lecture Two Functional Programming
14/24
FoldList
7/31/2019 SSG314 Lecture Two Functional Programming
15/24
In the last example, a Lambda function is folded as
shown. You can see the power of these namelessfunctions and the intricate computations performedon the fly that would have required a number ofiterations.
FoldList
7/31/2019 SSG314 Lecture Two Functional Programming
16/24
Once the idea of FoldList is fully understood,
remember that often we are interested only in thefinal fold result. In the case of the penultimateexample, we may be interested only in the last sum.The Fold[] function gives that as in:
Fold
7/31/2019 SSG314 Lecture Two Functional Programming
17/24
Nest and NestList combo work in a similar way as theabove:
Nest, NestList
7/31/2019 SSG314 Lecture Two Functional Programming
18/24
Nest supplies the ultimate result
in a NestList with the sameparameters.
Nest[] as well as NestList[]repeatedly applies the Head to a
single argument while FoldApply[](s) to a list of arguments.
Nest
7/31/2019 SSG314 Lecture Two Functional Programming
19/24
We first define a more elaborate factorial functionthat admits non-integer values. A plot of this functionis shown and the DownValues show the search route
UpValues & DownValues
7/31/2019 SSG314 Lecture Two Functional Programming
20/24
With UpValues, we can redefine Mathematica
functions in new contexts. See the cookbook for a detailed example.
Upvalues
7/31/2019 SSG314 Lecture Two Functional Programming
21/24
A quick comparison of the attributes of the Plus and dividefunctions help to see the meaning of some
Orderless and Flat are absent from Divide. The first showthat Plus is commutative in its arguments while Divide isnot. Further, Plus (f[f[x,y],z]) can be flattened out (f[x,y,z])
while Divide cannot. Listable shows that function can be supplied with a List
and it will be treaded over such a list: for example,Log[{1,3,6}] will create the list {Log[1],Log[3},Log[6]}
Attributes
7/31/2019 SSG314 Lecture Two Functional Programming
22/24
NumericFunction shows that when numeric
arguments are passed to the function, Mathematicacan assume the function itself is Numeric.
Protected shows that the values supplied areprotected from modification.
Others
7/31/2019 SSG314 Lecture Two Functional Programming
23/24
Sometimes you may want Mathematica to hold an
argument in the function without evaluation. This is anadvanced usage may not be obvious now. HoldFirst Mathematica defines a function Hold which prevents
its argument from being evaluated. The attribute HoldFirstallows you to give this feature to the first argument of afunction. All remaining arguments will behave normally.
HoldRest This is the opposite of HoldFirst; the first argumentis evaluated normally, but all remaining arguments are kept inunevaluated form.
HoldAll All arguments of the function are kept unevaluated.This is equivalent to using both HoldFirst and HoldRest.
Holding Arguments
7/31/2019 SSG314 Lecture Two Functional Programming
24/24