Functional Programming Lecture 2 - Functions - PowerPoint PPT Presentation

About This Presentation
Title:

Functional Programming Lecture 2 - Functions

Description:

Functional Programming. Lecture 2 - Functions. Muffy ... 'f takes an Int and returns an Int; the value of f x is x*100' The mantra: comment, type, equation ... – PowerPoint PPT presentation

Number of Views:20
Avg rating:3.0/5.0
Slides: 19
Provided by: muffyc
Category:

less

Transcript and Presenter's Notes

Title: Functional Programming Lecture 2 - Functions


1
Functional ProgrammingLecture 2 - Functions
  • Muffy Calder

2
Function Definitions
  • f Int -gt Int
  • -- multiply argument by 100
  • f x x100
  • f takes an Int and returns an Int the value of
    f x is x100
  • The mantra comment, type, equation

3
Defining Equation of Function
  • Fn_name arg1 arg2 argn
  • expression that can use
  • arg1argn
  • E.g. g x y x y gt 10

4
Evaluation or Simplification
  • Given
  • f x y xy
  • Evaluate f 3 4
  • f 3 4 gt 3 4 12
  • Now evaluate f 3 (f 4 5)
  • f 3 (f 4 5)gt 3 f 4 5gt 3 (45)gt 3
    20gt 60
  • Note there is an alternative evaluation!
  • Does it matter which one we pick? (Stay tuned)

5
Programs (Scripts)
  • What is meant by given?
  • A set of definitions is saved in a file.
  • E.g.
  • x 2
  • y 4
  • z 100x -30y
  • f Int -gt Int -gt Int
  • f x y x 2y
  • You use a program by asking it to perform a
    calculation, e.g. calculate f 2 3.
  • gt f 2 3

6
Equations vs. Assignments
  • Assignment - command to update a variable
  • e.g. x x1
  • x ------? ----? 3
  • Equation - permanent assertion that two values
    are equal e.g. x3 or x y1
  • x -----? 3
  • You obey assignments and solve equations.
  • Equations in a script can appear in any order!

7
and
  • Ada x x1
  • take the old value of x and increment it
  • This is a common and useful operation.
  • Haskell x x1
  • Assert that x and x1 are the same value.
  • This equation has no solution.
  • It is a syntactically-valid statement, but it is
    not solvable. There is no solution for x. So
    this equation cannot be true!

8
Side Effects andEquational Reasoning
  • Equations are timeless -there is no notion of
    changingthe value of a variable
  • Mathematics (algebra) is all about reasoning with
    equations, not with the contents of computer
    locations -- that keep changing.

9
Local functions where
  • Sometimes we want to make a function definition
    local (as opposed to global)
  • For example
  • f x y (g x) y
  • where
  • g z z 32
  • The function g is local to the function f.
  • Alternative
  • f x y (g x) y
  • g z z 32
  • h x (g x) y
  • In this case, the function g is available to
    other functions.

10
Local names let expressions
  • let x 1
  • y 2
  • z 3
  • in xyz
  • The variables x, y, z are local
  • A script is just one big let expression.

11
A let expression is an expression!
  • E.g.
  • 2 let a xy in (a1)(a-1)
  • 2 (let a 5 in (a1)(a-1) ) 3
  • - allows you to make somelocal definitions
  • - it is not a block of statements to be executed.

12
Conditional expressions
  • if boolean_exp
  • then exp1 else exp2
  • Haskell evaluates boolean_exp and returns the
    appropriate expression.
  • exp1 and exp2 must have the same type
  • E.g.
  • f x if xgt3 then x2 else x-2
  • g x if xgt3 then x2 else False (wrong)
  • What are the types of f and g?

13
Guarded expressions
  • f x
  • xgt0 True
  • otherwise False
  • In general
  • Name x1 x2 .. xn
  • guard1 e1
  • guard2 e2
  • .
  • otherwise e
  • Haskell evaluates the guards, from the top, and
    returns the appropriate expression.
  • guard1, ... must have the Bool type
  • exp1, ... must have the same type
  • Note
  • f x
  • xgt0 True
  • otherwise 2 (wrong)

14
Examples
  • positive Int -gt Bool
  • positive x
  • (xgt0) True
  • otherwise False
  • or
  • positive x if (xgt0) then True else False
  • max Int -gt Int -gt Int
  • max x y
  • (xgty) x
  • otherwise y
  • or
  • max x y if (xgty) then x else y
  • maxthree Int -gt Int -gt Int -gt Int
  • maxthree x y z
  • ((xgty) (xgtz) x
  • (ygtz) y
  • otherwise z

15
Examples
  • or
  • maxthree x y z max x (max y z)
  • mystery Int -gt Int -gt Int -gt Bool
  • mystery m n p not ((mn) (np))
  • Evaluate
  • mystery 0 1 2
  • mystery 0 1 1
  • mystery 1 1 1

16
Recursion
  • Functions can be recursive (i.e. the function can
    occur on the rhs of the defining equation).
  • This is fundamental to functional programming.
  • Example
  • fact Int -gt Int
  • fact 1 1
    -- fact.0
  • fact n n (fact n-1)
    -- fact.1
  • So, fact 4 gt 4 (fact 3)
  • gt 4 3 (fact 2)
  • gt 4 3 2 (fact 1)
  • gt 4 3 2 1
  • gt 24
  • fact.0 is called the base case.

17
Off-side Rule
  • mystery Int -gt Int -gt Int -gt Bool
  • mystery m n p not ((mn) (np))
  • the box
  • Whatever is typed in the box is part of the
    definition.
  • So,
  • f Int -gt Int
  • f x 42 --okay
  • g Int -gt Int --(start new
    definition)
  • g x --okay
  • 36
  • h Int -gt Int
  • h y 42 --okay
  • 1
  • hy

18
Functional Data Structures
  • Allow you to build aggregate objects from smaller
    values
  • Built-in data structures
  • Lists
  • Tuples
  • Arrays
  • User defined data structures
Write a Comment
User Comments (0)
About PowerShow.com