Functions - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

Functions

Description:

Function name. Input value. Domain Type. Co-Domain Type. Output ... Exercises: Write and test the following function definitions ... – PowerPoint PPT presentation

Number of Views:33
Avg rating:3.0/5.0
Slides: 17
Provided by: felix71
Category:

less

Transcript and Presenter's Notes

Title: Functions


1
Functions
The University of the West Indies
2
Functions
  • In Mathematics, a function f associates each
    member of a set A ( the domain of f ) with the
    single member of a set B ( the co-domain of f )
    which we write as
  • f A ? B
  • If a ? A then f(a) is the associated member of B.

3
Functions
  • Example
  • f(x) x2 for f R ? R
  • Haskell uses the same concept and similar
    notation to represent functions.

Set of Real Numbers
4
Functions
f(x) x2 for f R ? R
becomes
Domain Type
Co-Domain Type
f Float ? Float f x x2
Output
Input value
Function name
5
Functions
  • A notational difference in Haskell to be aware of
    is that we write
  • f x instead of f (x)
  • The brackets are not required and can make for
    clearer notation.

6
Functions
  • Be careful
  • square x1
  • is interpreted by Haskell as
  • (square x) 1

7
Functions
  • Remember
  • Function application has the highest precedence (
    priority ) than any other operator and is
    left-associative.

8
Functions
  • Another Example

add Float ? Float ? Float -- add two inputs x
and y add x y x y
Comments begin with -- and the line is ignored by
Haskell
9
Functions
  • Firstly we give the type of the function
  • Read as add takes two inputs of type Float and
    returns a value of type Float

add Float ? Float ? Float
Function name
Output Argument Type
Input Argument Types
10
Functions
  • Secondly we give the specification ( or
    post-condition ) of the function
  • The specification describes what the function
    does in English for the benefit other people.

-- add two inputs x and y
11
Functions
  • Finally we give the equation for the function
  • The equation describes how to calculate a result
    for the function.

add x y x y
12
Functions
  • The separates the left-hand side from the
    right-hand side ( the body )
  • The left-hand side gives the name of the function
    and the name(s) of its argument(s)
  • The right-hand side of the equation gives the
    expression that describes how the result of the
    function is calculated from the values of the
    argument(s)

13
Functions
  • Note
  • Choose meaningful names for your functions
  • Function names must begin with a lowercase letter
  • Do not use a function name that has already been
    used.

14
Functions
  • Once a function has been defined, we can apply it
    to given argument(s), provided the argument(s)
    have the right type.

Preludegt add 5.6 7.4 13.0
15
Functions
  • Example

distFromOrigin ( Float, Float) ?
Float distFromOrigin (x,y) sqrt ( x2 y2)
16
Functions
  • Exercises Write and test the following function
    definitions
  • A function to calculate the absolute difference
    of two integers ( use the if conditional )
  • A function to test if an input integer is even
  • ( hint use the Haskell function mod )
Write a Comment
User Comments (0)
About PowerShow.com