CSc 372 - Comparative Programming Languages
2 : Functional Programming

Christian Collberg

Department of Computer Science

University of Arizona

1 Programming Paradigms

2 Programming Paradigms...

A programming paradigm

Being familiar with several paradigms makes you a better programmer and problem solver. The most popular paradigms:

  1. Imperative programming.
  2. Functional programming.
  3. Object-oriented programming.
  4. Logic Programming.

When all you have is a hammer, everything looks like a nail.

3 Programming Paradigms...

Imperative Programming

Functional Programming

4 Programming Paradigms...

Object-Oriented Programming

Logic Programming

5 Procedural Programming

We program an abstraction of the Von Neumann Machine, consisting of a store (memory), a program (kept in the store), A CPU and a program counter (PC):

 
Store
Computing x:=x+1
  1. Compute x's address, send it to the store, get x's value back.
  2. Add 1 to x's value.
  3. Send x's address and new value to the store for storage.
  4. Increment PC.

6 Procedural Programming...

The programmer...


\begin{lstlisting}[language=Pascal]
function fact (n:integer):integer;
var s,i ...
...egin
while i<=n do s:=s*i; i:=i+1; end;
return s;
end fact.
\end{lstlisting}

7 Procedural Programming...

Procedural programming is difficult because:

  1. A procedural program can be in a large number of states. (Any combination of variable values and PC locations constitutes a possible state.) The programmer has to keep track of all of them.
  2. Any global variable can be changed from any location in the program. (This is particularly true of languages like C & C++ [Why?]).
  3. It is difficult to reason mathematically about a procedural program.


Functional Programming


8 Functional Programming

In contrast to procedural languages, functional programs don't concern themselves with state and memory locations. Instead, they work exclusively with values, and expressions and functions which compute values.

9 Functional Languages

Common characteristics of functional programming languages:

  1. Simple and concise syntax and semantics.
  2. Repetition is expressed as recursion rather than iteration.
  3. Functions are first class objects. I.e. functions can be manipulated just as easily as integers, floats, etc. in other languages.
  4. Data as functions. I.e. we can build a function on the fly and then execute it. (Some languages).

10 Functional Languages...

  1. Higher-order functions. I.e. functions can take functions as arguments and return functions as results.
  2. Lazy evaluation. Expressions are evaluated only when needed. This allows us to build infinite data structures, where only the parts we need are actually constructed. (Some languages).
  3. Garbage Collection. Dynamic memory that is no longer needed is automatically reclaimed by the system. GC is also available in some imperative languages (Modula-3, Eiffel) but not in others (C, C++, Pascal).

11 Functional Languages...

  1. Polymorphic types. Functions can work on data of different types. (Some languages).
  2. Functional programs can be more easily manipulated mathematically than procedural programs.

Pure vs. Impure FPL


Specifying Functions


12 What is a function?

Capitals

13 More on functions

14 More on functions...

15 More on functions...

16 Specifying functions

A function can be specified extensionally or intentionally.

Extensionally:


\begin{gprogram}
double = \{$\cdots$, (1,2), (5,10), $\cdots$\} \\
even = \{$\c...
...cdots$\} \\
isHandsome=\{Chris$\mapsto$True,Hugh$\mapsto$False\}
\end{gprogram}

17 Specifying functions...

Intensionally:


\begin{gprogram}
double x = 2 * x \\
even x = x mod 2 == 0 \\
isHandsome x = \...
...isBald x \\
\xxx \redtxt{then} True \\
\xxx \redtxt{else} False
\end{gprogram}

18 Specifying functions...

Graphically:

EitherEven

19 Function Application


\begin{gprogram}
double x = 2 * x \\
even x = x mod 2 == 0 \\
\\
double 5 $\Rightarrow$\ 10 \\
even 6 $\Rightarrow$\ True
\end{gprogram}

20 Function Composition


\begin{gprogram}
double x = 2 * x \\
even x = x mod 2 == 0 \\
\\
even (double 5) $\Rightarrow$\ even 10 $\Rightarrow$\ True
\end{gprogram}

21 Function Definition -- Example

Example: How many numbers are there between $m$ and $n$, inclusive?

Extensional Definition:
\begin{gprogram}
sumbetween m n = $\{\cdots$\ $(1,1)\mapsto1, (1,2)\mapsto2, \cdots, (2,10)\mapsto9\}$
\end{gprogram}

Intentional Definition:
\begin{gprogram}
sumbetween m n = ((m + n) * (abs (m-n) + 1)) div 2
\end{gprogram}

Graphical Definition:

SumBetween

22 Function Signatures

To define a function we must specify the types of the input and output sets (domain and range, i.e. the function's signature), and an algorithm that maps inputs to outputs.

FunctionTypes


What's so Good About FP?


23 Referential Transparency

24 Referential Transparency...

25 Referential Transparency...

26 Referential Transparency...

27 Referential Transparency...

28 Referential Transparency...

29 Referential Transparency...

30 Referential Transparency...

Evaluate \fbox{\tt even (double 5)}:


\begin{gprogram}
\xx double x = 2 * x \\
\xx even x = x mod 2 == 0 \\
\\
\\
...
...x 10 mod 2 == 0 $\Rightarrow$\ \\
\xx 0 == 0 $\Rightarrow$\ True
\end{gprogram}

31 Referential Transparency...

In a pure functional language

  1. Expressions and sub-expressions always have the same value, regardless of the environment in which they're evaluated.
  2. The order in which sub-expressions are evaluated doesn't effect the final result.
  3. Functions have no side-effects.
  4. There are no global variables.

32 Referential Transparency...

  1. Variables are similar to variables in mathematics: they hold a value, but they can't be updated.
  2. Variables aren't (updatable) containers the way they are imperative languages.
  3. Hence, functional languages are much more like mathematics than imperative languages. Functional programs can be treated as mathematical text, and manipulated using common algebraic laws.

33 Homework

\begin{eqnarray*}
\left(\begin{array}{c} n \\ r \end{array} \right) & = & \frac{n!}{r!*(n-r)!}
\end{eqnarray*}



Christian S. Collberg
2005-08-22