Title: Programming Paradigms via Mathematica
1Programming Paradigms via Mathematica
- Dr Dejan Toic, ETF Beograd tosic_at_etf.bg.ac.yu
http//kondor.etf.bg.ac.yu/tosic/
2Mathematica
- Mathematica is the world's only fully integrated
environment for technical computing. - The visionary concept of Mathematica was to
create once and for all a single system that
could handle all the various aspects of
technical computing in a coherent and unified
way. - The key intellectual advance that made this
possible was the invention of a new kind of
symbolic computer language that could for the
first time manipulate the very wide range of
objects involved in technical computing using
only a fairly small number of basic primitives.
3Mathematica combines ideas from many different
sources
- LISP Mathematica can manipulate lists using the
data structures of the programming language LISP.
- PROLOG Mathematica uses pattern matching
capabilities taken from PROLOG to express
mathematical rules in a simple way. - APL Mathematica's simple manipulation of
structured data has been pioneered by APL. - SIMULA, SMALLTALK, and C Object-oriented
elements make code development easier. - PASCAL, FORTRAN, and C Traditional procedural
programming style. - Other Features Modularization and Built-In
Functions.
4Software development cycle
5Square brackets , Curly braces , and
Parentheses ( )
- Parentheses ( ) are used in Mathematica for
grouping in arithmetic. - Square brackets are used for enclosing the
arguments of functions. - Curly braces are used for entering lists.
- Using square brackets to enclose function
arguments avoids the ambiguity in traditional
mathematical notation between parentheses that
enclose function arguments and parentheses that
are used for grouping arithmetic operations.
6Normal Expression
- A normal expression is an expression that has a
head and elements (arguments). - The head of the normal expression fa,b, for
example, is f, and the elements are a and b. - The head and the elements in a normal expression
are also expressions, and can, for example, be
other normal expressions.
7Atoms
- Expressions that are not normal expressions are
called atoms. - Atoms are either
- symbols,
- numbers, or
- strings.
- All expressions are constructed from atoms.
8Patterns
- A pattern is an expression that is used to
specify a set of expressions. - Any expression in the set of expressions
specified by a pattern is said to match the
pattern. - The simplest non-trivial pattern is Blank,
which matches any expression. Blank is usually
entered as a single underscore character _. - For example, the pattern _,_ matches any list
with two elements.
9Canonical example
10Built-in Functions
One way to program a function that computes the
factorial of a non-negative integer is to simply
use the built-in function Factorial.
11Composition of Functions Anonymous (pure)
function
12Procedural FORTRANesque programming Do
13Straight-forward implementation Product
14Functional programming
15Range generates a list of consecutive integers
16Times multiplies together its arguments
17Rule-based programming
18Transformation rules
- Rules are a mechanism for replacing a variable
with a value in an expression. - A rule can transform one expression, lhs, into
another expression, rhs lhs -gt rhs
19Rule-based programming style
- The transformations are described by rules which
separate the description of the sub-object to be
replaced (the pattern) from the calculation of
the replacement. - Optionally, rules can have further conditions
that restrict their applicability. - The transformations are controlled by explicit or
implicit strategies.
20Mixed programming Definitions, Rules, Patterns
21Dynamically Stored Results
22Information on objects ? command
23Recursive programming
24C-style programming For
25Iterative programming While
26Traditional form Product
27Definition using pure function
28Benefits
- Visionary concept of advanced computing
- Fully integrated mathematical environment
- Symbolic computer language
- Various programming paradigms
- Short software development cycle