180 likes | 422 Views
Functional Programming Basics. Correctness > Clarity > Efficiency. Function Definition Equations ; Recursion Higher-order functions Function Application Computation by expression evaluation Choices : parameter passing Reliability Types Strong typing, Polymorphism, ADTs.
E N D
Functional Programming Basics Correctness > Clarity > Efficiency L1FP
Function Definition • Equations ; Recursion • Higher-order functions • Function Application • Computation by expression evaluation • Choices : parameter passing • Reliability • Types • Strong typing, Polymorphism, ADTs. • Garbage Collection L1FP
Imperative Style vs Functional Style • 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; L1FP
GCD : functional vs imperative 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
Pitfalls : Sequencing (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
Function • 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
Representation of functions • 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
Expression Evaluation : Reduction 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. Role of variable L1FP
Referential Transparency • 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
Examples 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) • Parallel evaluation of sub-expressions possible with referential transparency. L1FP
Strict vs Non-strict • 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
Benefits of Programming in a Functional Language • Convenient to code symbolic computations and list processing applications. • Automatic storage management • Improves program reliability. • Enhances programmer productivity. • Abstraction through higher-order functions and polymorphism. • Facilitates code reuse. • Ease of prototyping using interactive development environments. L1FP
Summary 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