Functional Programming - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

Functional Programming

Description:

Concepts from Functional Programming Languages Peter Gorm Larsen Agenda Introduction to the Functional Programming Paradigm The notion of higher order functions ... – PowerPoint PPT presentation

Number of Views:209
Avg rating:3.0/5.0
Slides: 21
Provided by: PeterG172
Category:

less

Transcript and Presenter's Notes

Title: Functional Programming


1
Concepts from Functional Programming Languages
  • Peter Gorm Larsen

2
Agenda
  • Introduction to the Functional Programming
    Paradigm
  • The notion of higher order functions
  • Polymorphic examples of standard higher order
    functions

3
Introduction to FP
  • The design of the imperative languages is based
    directly on the von Neumann architecture
  • Efficiency is the primary concern, rather than
    the suitability of the language for software
    development
  • The design of the functional languages is based
    on mathematical functions
  • A solid theoretical basis that is also closer to
    the user, but relatively unconcerned with the
    architecture of the machines on which programs
    will run

4
Principles of FP
  • Treats computation as evaluation of mathematical
    functions (and avoids state)
  • Data and programs are represented in the same way
  • Functions as first-class values
  • Higher-order functions functions that operate
    on, or create, other functions
  • Functions as components of data structures
  • Lambda calculus provides a theoretical framework
    for describing functions and their evaluation
  • It is a mathematical abstraction rather than an
    imperative programming language

5
History
  • lambda calculus (Church, 1932)
  • simply typed lambda calculus (Church, 1940)
  • lambda calculus as prog. lang. (McCarthy(?),
    1960, Landin 1965)
  • polymorphic types (Girard, Reynolds, early 70s)
  • algebraic types ( Burstall Landin, 1969)
  • type inference (Hindley, 1969, Milner, mid 70s)
  • lazy evaluation (Wadsworth, early 70s)
  • Equational definitions Miranda 80s
  • Type classes Haskell 1990s
  • Microsoft F etc 2000s

6
Varieties of FP languages
  • Typed (ML, Haskell) vs untyped (Scheme, Erlang)
  • Pure vs Impure
  • impure have state and imperative features
  • pure have no side effects, referential
    transparency
  • Strict vs Lazy evaluation

7
Declarative style of programming
  • Declarative Style of programming - emphasis is
    placed on describing what a program should do
    rather than prescribing how it should do it.
  • Functional programming - good illustration of the
    declarative style of programming.
  • A program is viewed as a function from input to
    output.
  • Logic programming another paradigm
  • A program is viewed as a collection of logical
    rules and facts (a knowledge-based system). Using
    logical reasoning, the computer system can derive
    new facts from existing ones.

8
Functional style of programming
  • A computing system is viewed as a function which
    takes input and delivers output.
  • The function transforms the input into output .
  • Functions are the basic building blocks from
    which programs are constructed.
  • The definition of each function specifies what
    the function does.
  • It describes the relationship between the input
    and the output of the function.

9
Agenda
  • Introduction to the Functional Programming
    Paradigm
  • The notion of higher order functions
  • Polymorphic examples of standard higher order
    functions

10
First-Class Functions
  • Data values are first-class if they can
  • be assigned to local variables
  • be components of data structures
  • be passed as arguments to functions
  • be returned from functions
  • be created at run-time

11
Higher-order Functions
  • Every function has an order
  • A function that does not take any functions as
    parameters, and does not return a function value,
    has order 1
  • A function that takes a function as a parameter
    or returns a function value has order n1, where
    n is the order of its highest-order parameter or
    returned value
  • A small example
  • Twice (int -gt int) int -gt int
  • Twice(f,x)
  • f( f (x))
  • Or
  • TwiceCur (int -gt int)-gt int -gt int
  • TwiceCur(f)(x)
  • f( f (x))

12
Functions in Programming Languages
  • How functions are treated by programming
    languages?

Language passed as arguments returned from functions nested scope
Java No No No
C Yes Yes No
C Yes Yes No
Pascal Yes No Yes
Modula-3 Yes No Yes
Scheme Yes Yes Yes
ML Yes Yes Yes
13
Nested Functions and Closures
  • Return a function from function call
  • function f(x)
  • var y x
  • return function (z)y z return y
  • var h f(5)
  • h(3)
  • In order to handle this one needs to introduce
    closures
  • A closure is a function that captures the
    bindings of free variables in its lexical context.

14
Agenda
  • Introduction to the Functional Programming
    Paradigm
  • The notion of higher order functions
  • Polymorphic examples of standard higher order
    functions

15
Predefined Higher-Order Functions in Functional
Languages
  • We will use three important predefined
    higher-order functions
  • map
  • filter
  • foldr
  • foldl
  • Actually, foldr and foldl are very similar, as
    you might guess from the names

16
The Map Function
  • Map applies a function to every element of a
    list
  • Map_at_A,_at_B (_at_A -gt _at_B) -gt seq of _at_A -gt seq of _at_B
  • Map(f)(list)
  • f(list(i)) i in set inds list

17
The Filter Function
  • Filter selects every element that satisfies a
    predicate
  • Filter_at_A (_at_A -gt bool) -gt seq of _at_A -gt seq of
    _at_A
  • Filter(pred)(list)
  • list(i) i in set inds list pred(list(i))

18
The FoldR Function
  • Folds all elements in a list from the right into
    one value (a simple pattern of recursion)
  • FoldR_at_A,_at_B (_at_A _at_B -gt _at_B) -gt _at_B -gt seq of _at_A
    -gt _at_B
  • FoldR(f)(neutral)(list)
  • if list
  • then neutral
  • else f(hd list,FoldR(f)(neutral)(tl list))
  • Example usage
  • Sum FoldR()(0)
  • Product FoldR()(1)
  • Or FoldR(or)(false)
  • And FoldR(and)(true)

19
Summary
  • What have I presented today?
  • Introduction to the Functional Programming
    Paradigm
  • The notion of higher order functions
  • Polymorphic examples of standard higher order
    functions
  • What do you need to do now?
  • Complete your distributed real time model for
    your project

20
Quote of the day
  • Program designers have a tendency to think of the
    users as idiots who need to be controlled. They
    should rather think of their program as a
    servant, whose master, the user, should be able
    to control it. If designers and programmers think
    about the apparent mental qualities that their
    programs will have, they'll create programs that
    are easier and pleasanter more humane to deal
    with.
  • John McCarthy
Write a Comment
User Comments (0)
About PowerShow.com