FUNCTIONAL (APPLICATIVE) VS. IMPERATIVE PROGRAMMING
Functional (Applicative)
Computes values by calling functions:
let fac n =
facing 1 n ;;
let rec facing F t =
if t > 0
then Facing (f + t) (t - 1)
else f ;;
● Implicit Type Casting - the programming language automatically converts data from one
type to another
● Expression language - everything in the program is an expression that evaluates to a
value
● No return statement - a function evaluates to its expression (result)
● Loops become recursions - a function calls itself recursively
● Imperative variables turn into applicative parameters - values are passed as parameters
through recursive function calls
● NOT Sequential
● Function calls don’t use () and no variables are used
Imperative
Issue commands to change variables
int fac (int n)
{
int f = 1 ;
int t = n ;
while (t > 0)
{
f = f * t ;
t = t - 1 ;
}
return f ;
}
● Explicit Type Casting - the programmer explicitly converts a value from one data type to
another.
● Uses variables (e.g. f, t, and n) - explicitly stated and assigned values
● Statement language - everything in the program is built from statements
● Variable assignment - variables are explicitly stated, assigned, and reassigned
● Return statement - required to produce a value from a function, explicitly stating what
value the function returns
● Sequentially executed - statements execute top to bottom in order