functional programming basics
Download
Skip this Video
Download Presentation
Functional Programming Basics

Loading in 2 Seconds...

play fullscreen
1 / 18

Functional Programming Basics - PowerPoint PPT Presentation


  • 146 Views
  • Uploaded on

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.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Functional Programming Basics' - rowa


An Image/Link below is provided (as is) to download presentation

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 - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
functional programming basics

Functional Programming Basics

Correctness > Clarity > Efficiency

L1FP

slide2
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 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

slide4
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
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
Pitfalls : Sequencing

(define (factorial n)

(define (iter prod counter)

(if (> counter n) prod

(iter (* counter prod)

(+ counter 1) ) ))

(iter 1 1)

)

L1FP

slide7
(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
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
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
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

role of variable
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
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
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

slide14
(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

slide15
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
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
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
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

ad