Download arguments (an upper and lower bound (integers) and a function

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Combinatory logic wikipedia , lookup

Lambda calculus wikipedia , lookup

Lambda lifting wikipedia , lookup

Anonymous function wikipedia , lookup

Closure (computer programming) wikipedia , lookup

Currying wikipedia , lookup

Standard ML wikipedia , lookup

Transcript
LAB functional programming CSCU9Y4 2016
In lecture we talked about higher order functions. This means that we can pass around functions just
as we pass around other data types. For example in maths we have:
𝑖=10
∑ 𝑓(𝑥)
𝑖=1
Where the sign ∑ means sum (f(1)+f(2)+…+f(10)). Summation is a higher order function which takes 3
arguments; the upper and lower bounds (which are integers) and a function f (which is to be
summed).
The function below takes 3 parameters (f, a, and b). Here f is a function and a and b are lower and
upper bounds respectively, and f is the function to be summed over.
#to calculate the sum of f from a to b
def sum(f, a, b):
total = 0
for i in range(a, b+1):
total += f(i)
return total
Question 1. Type in the above code and calculate the sum of integers from 1 to 10, and 1 to 100 and
1 to 1000
Question 2. Modify the above code to do product. E.g. π. Similar to summation, product take 3
arguments (an upper and lower bound (integers) and a function mapping integers to
𝑖=3
integers). ∏𝑖=3
𝑖=1 𝑓(𝑥) means f(1)*f(2)*f(3). For example ∏𝑖=1 𝑥 means 1*2*3 (this
𝑖=10
𝑖=20
function is usually called factorial). Calculate ∏𝑖=5
𝑖=1 𝑥 , ∏𝑖=1 𝑥 , ∏𝑖=1 𝑥 ,
Question 3. Modify the code above to numerically integrate between the lower bound a and the
upper bound b. Hint, this is done by adding another parameter, width. The integral can then be
calculated by summing up lots of rectangles of with area (width*f(x)). The narrower the width, the
better the approximation is.
Calculate the integrals of f(x)=x from 0 to 100,
Calculate the integrals of f(x)=x*x from 0 to 100,
Calculate the integrals of f(x)= sin(x) from 0 to pi,
Calculate the integrals of f(x)= sin(x)* sin(x) from 0 to pi.
(Note you may have to Google to find out how sin and pi work in Python. )
Question 3. In lectures we looked at partial functions (i.e. we only supplied the first few arguments
and returned a function which accepted the remaining arguments). In other words it allows you to
build new functions from existing functions by supplying some of the arguments. Two examples are
given below.
#makes a function inc (increment) with one argument from the partial application
of add with one argument supplied (1). That is inc(x)=add(1,x)
from functools import partial
def add(a,b):
return a+b
inc = partial(add, 1)
print inc(4)
# makes a function double with one argument from the partial application of mul
with one argument supplied (2). That is double(x)=mul(2,x)
def mul(a,b):
return a*b
double = partial(mul, 2)
print double(10)
Now write a function called half which takes one argument and halves it. For
example half(6)=3, half(10)=5. Hint, first construct a function called div which
takes two arguments and divides one by the other. Ask me if you need another hint.
Question 4.
A quadratic equation is f(x) = a*x*x + b*x + c (where a, b, c are constants and x
is the variable).
Often we want to solve f(x) = 0. That is a*x*x + b*x + c = 0.
Note that this can be written as r(x-p)(x-q) = (r)*x*x + b*x + c = 0
The solution to is given by
def solveQuad(a, b, c):
return (-b+math.sqrt(b*b-4*a*c ))/(2*a)
#note that we ignore the second root with this.
A linear equation y= a*x + b is a special case of a quadratic equation. Write a linear solver in terms of
def solveQuad(a, b, c) using partial application. Hint, if you set c=0 in (a*x*x +
b*x + c = 0) and divide by x you get a*x + b, and if you set c=0 in (-b+math.sqrt(b*b4*a*c ))/(2*a) what do you get.
Question 5.
Implement your own map, filter and reduce functions. You cannot use the builtin
versions of map, filter and reduce. Check yours give the same answers as the
builtin.
def myReduce(list, function):
def myMap(list, function):
def myFilter(list, function):
First checkpoint – show a lab demonstrator your code for questions 1 and 2. Run
the code on the three examples.
Second checkpoint - show a lab demonstrator your code for questions 3 and 4.