LAB functional programming CSC9Y4 (SCALA) In lecture we talked

advertisement
LAB functional programming CSC9Y4 (SCALA)
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.
def sum(f: Int => Int, a: Int, b: Int): Int = {
if (a > b) 0
else f(a) + sum(f, a + 1, b)
}// to calculate the sum of f from a to b
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 function is
𝑖=10
𝑖=20
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. )
Question. 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)
def add(x: Int, y: Int) = x + y
val inc = add(1, _: Int)
println(inc(9))
// makes a function dub with one argument from the partial application of mul with one
argument supplied (2). That is dub(x)=mul(2,x)
def mul(x: Int, y: Int):Int = {x * y}
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.
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: Double, b: Double, c: Double): Double = {
(-b - Math.sqrt(b * b - 4.0 * a * c)) / (2.0 * 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 solveQuad(a: Double, b: Double, c: Double) 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*b-4*a*c ))/(2*a) what do you get. Another hint val solveLinear =
solveQuad(?????????????????), you need to fill in the “??????”
Question.
Informally the floor function takes a number and rounds it down to the nearest whole number
(e.g. floor(9.5)=9), what is the type signature for this function. Explain your answer.
Question.
One of the first programs many people write is: given a pair of numbers, return the
maximum. We are going to do the same but with functions.
In lectures we maxFunction which takes two functions as arguments, and returns a function
which was the maximum of the pair. A picture might help you do this.
Write a function which takes two functions, say mapping Double to Double, and returns a
function also mapping Double to Double which is the minimum of the pair.
Question. Can you give an example of a higher order function not used in lectures?
Question. Implement the higher order functions (map, filter, reduce).
Download