functional programming lecture 2 - functions
DESCRIPTION
Functional Programming Lecture 2 - Functions. Muffy Calder. Function Definitions. f :: Int -> Int -- multiply argument by 100 f x = x*100 “f takes an Int and returns an Int; the value of f x is x*100” The mantra: comment, type, equation. Defining Equation of Function. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Functional Programming Lecture 2 - Functions](https://reader036.vdocuments.us/reader036/viewer/2022082817/56812a64550346895d8ddc37/html5/thumbnails/1.jpg)
Functional ProgrammingLecture 2 - Functions
Muffy Calder
![Page 2: Functional Programming Lecture 2 - Functions](https://reader036.vdocuments.us/reader036/viewer/2022082817/56812a64550346895d8ddc37/html5/thumbnails/2.jpg)
Function Definitions
f :: Int -> Int
-- multiply argument by 100
f x = x*100
“f takes an Int and returns an Int; the value of f x is x*100”
The mantra: comment, type, equation
![Page 3: Functional Programming Lecture 2 - Functions](https://reader036.vdocuments.us/reader036/viewer/2022082817/56812a64550346895d8ddc37/html5/thumbnails/3.jpg)
Defining Equation of Function
Fn_name arg1 arg2 … argn
= expression that can use
arg1…argn
E.g. g x y = x + y >= 10
![Page 4: Functional Programming Lecture 2 - Functions](https://reader036.vdocuments.us/reader036/viewer/2022082817/56812a64550346895d8ddc37/html5/thumbnails/4.jpg)
Evaluation or Simplification
Given: f x y = x*y
Evaluate: f 3 4f 3 4 => 3 * 4 = 12
Now evaluate: f 3 (f 4 5)
f 3 (f 4 5)=> 3 * f 4 5=> 3 * (4*5)=> 3 * 20=> 60
Note there is an alternative evaluation!Does it matter which one we pick? (Stay tuned)
![Page 5: Functional Programming Lecture 2 - Functions](https://reader036.vdocuments.us/reader036/viewer/2022082817/56812a64550346895d8ddc37/html5/thumbnails/5.jpg)
Programs (Scripts)
What is meant by “given”?
A set of definitions is saved in a file.
E.g.
x = 2
y = 4
z = 100*x -30*y
f :: Int -> Int -> Int
f x y = x + 2*y
You use a program by “asking” it to perform a calculation, e.g. calculate f 2 3.
> f 2 3
![Page 6: Functional Programming Lecture 2 - Functions](https://reader036.vdocuments.us/reader036/viewer/2022082817/56812a64550346895d8ddc37/html5/thumbnails/6.jpg)
Equations vs. Assignments
Assignment - command to update a variable
e.g. x:= x+1
x ------ ---- 3
Equation - permanent assertion that two values are equal e.g. x=3 or x = y+1
x ----- 3
You obey assignments and solve equations.
Equations in a script can appear in any order!
![Page 7: Functional Programming Lecture 2 - Functions](https://reader036.vdocuments.us/reader036/viewer/2022082817/56812a64550346895d8ddc37/html5/thumbnails/7.jpg)
:= and =
Ada x := x+1
take the old value of x and increment it
This is a common and useful operation.
Haskell x = x+1
Assert that x and x+1 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!
![Page 8: Functional Programming Lecture 2 - Functions](https://reader036.vdocuments.us/reader036/viewer/2022082817/56812a64550346895d8ddc37/html5/thumbnails/8.jpg)
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.
![Page 9: Functional Programming Lecture 2 - Functions](https://reader036.vdocuments.us/reader036/viewer/2022082817/56812a64550346895d8ddc37/html5/thumbnails/9.jpg)
Local names: let expressions
let x = 1
y = 2
z = 3
in x+y+z
The variables x, y, z are local
A script is just one big let expression.
![Page 10: Functional Programming Lecture 2 - Functions](https://reader036.vdocuments.us/reader036/viewer/2022082817/56812a64550346895d8ddc37/html5/thumbnails/10.jpg)
A let expression is an expression!
E.g.
2 + let a = x+y in (a+1)*(a-1)
2 + (let a = 5 in (a+1)*(a-1) ) + 3
- allows you to make somelocal definitions
- it is not a block of statements to be executed.
![Page 11: Functional Programming Lecture 2 - Functions](https://reader036.vdocuments.us/reader036/viewer/2022082817/56812a64550346895d8ddc37/html5/thumbnails/11.jpg)
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 x>3 then x+2 else x-2
g x = if x>3 then x+2 else False (wrong)
What are the types of f and g?
![Page 12: Functional Programming Lecture 2 - Functions](https://reader036.vdocuments.us/reader036/viewer/2022082817/56812a64550346895d8ddc37/html5/thumbnails/12.jpg)
Guarded expressions
f x | x>0 = 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 typeexp1, ... must have the same type
Note: f x | x>0 = True | otherwise = 2 (wrong)
![Page 13: Functional Programming Lecture 2 - Functions](https://reader036.vdocuments.us/reader036/viewer/2022082817/56812a64550346895d8ddc37/html5/thumbnails/13.jpg)
Examplespositive :: Int -> Bool
positive x
| (x>=0) = True
| otherwise = False
or
positive x = if (x>=0) then True else False
max :: Int -> Int -> Int
max x y
| (x>=y) = x
| otherwise = y
or
max x y = if (x>=y) then x else y
maxthree :: Int -> Int -> Int -> Int
maxthree x y z
| ((x>=y) && (x>=z) = x
| (y>=z) = y
| otherwise = z
![Page 14: Functional Programming Lecture 2 - Functions](https://reader036.vdocuments.us/reader036/viewer/2022082817/56812a64550346895d8ddc37/html5/thumbnails/14.jpg)
Examples
or
maxthree x y z = max x (max y z)
mystery :: Int -> Int -> Int -> Bool
mystery m n p = not ((m==n) && (n==p))
Evaluate:
mystery 0 1 2
mystery 0 1 1
mystery 1 1 1
![Page 15: Functional Programming Lecture 2 - Functions](https://reader036.vdocuments.us/reader036/viewer/2022082817/56812a64550346895d8ddc37/html5/thumbnails/15.jpg)
RecursionFunctions 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 -> Int
fact 1 = 1 -- fact.0
fact n = n * (fact n-1) -- fact.1
So, fact 4 => 4 * (fact 3)
=> 4 * 3 * (fact 2)
=> 4 * 3 * 2 * (fact 1)
=> 4 * 3 * 2 * 1
=> 24
fact.0 is called the base case.
![Page 16: Functional Programming Lecture 2 - Functions](https://reader036.vdocuments.us/reader036/viewer/2022082817/56812a64550346895d8ddc37/html5/thumbnails/16.jpg)
Off-side Rule
mystery :: Int -> Int -> Int -> Bool
mystery m n p = not ((m==n) && (n==p))
the box
Whatever is typed in the box is part of the definition.
So,
f :: Int -> Int
f x = 42 --okay
g :: Int -> Int --(start new definition)
g x --okay
= 36
h:: Int -> Int
h y= 42 * --okay
1
hy =
42 -- will give an error (unexpected ‘;’)
![Page 17: Functional Programming Lecture 2 - Functions](https://reader036.vdocuments.us/reader036/viewer/2022082817/56812a64550346895d8ddc37/html5/thumbnails/17.jpg)
Functional Data Structures
Allow you to build aggregate objects from smaller values
• Built-in data structures
Lists
Tuples
Arrays
• User defined data structures