- 119 Views
- Uploaded on
- Presentation posted in: General

Functional Programming Basics

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Functional Programming Basics

Correctness > Clarity > Efficiency

L1FP

- Function Definition
- Equations ; Recursion
- Higher-order functions

- Choices : parameter passing

- Types
- Strong typing, Polymorphism, ADTs.

- Garbage Collection

L1FP

- Imperative programs
- Description of WHAT is to be computed is inter-twined with HOW it is to be computed.
- The latter involves organization of data and the sequencing of instructions.

- Functional Programs
- Separates WHAT from HOW.
- The former is programmer’s responsibility; the latter is interpreter’s/compiler’s responsibility.

L1FP

- Functional Style
- Value to be computed: a + b + c

- Imperative Style
- Recipe for computing the value
- Intermediate Code
- T := a + b; T := T + c;
- T := b + c; T := a + T;

- Accumulator Machine
- Load a; Add b; Add c;

- Stack Machine
- Push a; Push b; Add; Push c; Add;

- Intermediate Code

- Recipe for computing the value

L1FP

fun gcd(m,n) =

if m=0 then n

else gcd(n mod m, m);

function gcd(m,n: int) : int;

var pm:int;

begin while m<>0 do

begin

pm := m; m := n mod m; n := pm

end;

return n

end;

L1FP

(define (factorial n)

(define (iter prod counter)

(if (> counter n) prod

(iter (* counter prod)

(+ counter 1) ) ))

(iter 1 1)

)

L1FP

(define (factorial n)

(let ((prod 1)(counter 1))

(define (iter)

(if (> counter n) prod

(begin

(set! prod (* counter prod))

(set! counter (+ 1 counter))

(iter))

))

))

L1FP

- A function ffrom domain A to co-domain B, denoted f : A -> B, is a map that associates with every element a in A, a unique element b in B, denoted f(a).
- Cf. Relation, multi-valued function, partial function, …
- In mathematics, the term “function” usually refers to a total function; in computer science, the term “function” usually refers to a partial function.

L1FP

- Intensional : Rule of calculation
fun double n = 2 * n;

fun double n = n + n;

- Extensional : Behavioral (Table)
- Equality:
f = giff for all x: f(x) = g(x)

L1FP

fun double x = x + x;

double ( 3 * 2)

double(6) (3*2) + (3*2) (3*2) + o

6 + 6 6 + (3 * 2) 6 + o

Applicative-Order Normal-Order Lazy

(call by value) (call by name) (call by need)

L1FP

In functional style, a variable stands for an arbitrary value, and is used to abbreviate an infinite collection of equations.

0 + 0 = 0

0 + 1 = 1

…

for all x : 0 + x = x

In imperative style, a variable is a location that can hold a value, and which can be changed through an assignment.

x := x + 1;

Functional variable can be viewed as assign-only- once imperative variable.

L1FP

- The only thing that matters about an expression is its value, and any sub-expression can be replaced by any other expression equal in value.
- The value of an expression is independent of its position only provided we remain within the scopes of the definitions which apply to the names occurring in the expression.

L1FP

let x = 5 in

x + let x = 4 in x + x;

val y = 2; val y = 6;

var x : int;

begin

x := x + 2; x := x + 1;

end;

address of x value stored in location for x

L1FP

(x=2) /\ (x+y>2) (2+y>2)

vs

fun f (x : int) : int ;

begin y := y + 1;

return ( x + y)

end;

(y=0) /\ (z=0) /\ (f(y)=f(z))

= false

(y=0) /\ (z=0) /\ (f(z)=f(z))

=/= (y=0) /\ (z=0) /\ (f(z)=1)

L1FP

- Common sub-expression elimination is an “incorrect optimization” without referential transparency.
- In functional style:
E + E =let x = E in x + x

- In imperative style:
return (x++ + x++)

=/=

y := x++; return (y + y)

- In functional style:
- Parallel evaluation of sub-expressions possible with referential transparency.

L1FP

- A function is strict if it returns well-defined results only when the inputs are well-defined.
- E.g., In C, “+” and “*” are strict, while “&&” and “||” are not.
- E.g., In Ada, “and” and “or” are strict, while “and then” and “or else” are not.
- E.g., constant functions are non-strict if called by name, but are strict if called by value.

L1FP

- Convenient to code symbolic computations and list processing applications.
- Automatic storage management
- Improves program reliability.
- Enhances programmer productivity.

- Facilitates code reuse.

L1FP

Programming Languages

Imperative

Functional

Logic

C, Pascal

Prolog

Dynamically Typed

(Meta-programming)

Statically Typed

(Type Inference/Reliable)

LISP, Scheme

Lazy Eval /

Pure

Eager Eval

/ Impure

Haskell

SML

L1FP