Discussions 2
Discussions 2
Discussions 2
1
2
3
4
1
3
1
4
The numbers at the edge of the triangle are all 1, and each number inside the triangle is
the sum of the two numbers above it.
Write a function that computes elements of Pascals triangle by means of a recursive process, i.e. the function should take two arguments, row and column, and return the element
for the specified column and row. Note that only one element must be returned and NOT
the entire row. E.g. calling the method with row = 3 and column = 2 should return 2.
Likewise calling the method with row = 5 and column = 3 should return 6.
2. Draw the tree illustrating the process generated by the cc (count-change) function, in making change for 11 cents, using the denominations 50, 20, 10, 5 and 1. What are the orders
of growth of the space and number of steps used by this process as the amount to be
changed increases?
3. A function f is defined by the rule that f (n) = n if n < 3 and f (n) = f (n1)+2f (n2)+3f (n3)
if n 3.
(a) Write a function that computes f by means of a recursive process.
(b) Write a function that computes f by means of an iterative process.
4. Suppose we define the function:
function f(g) {
return g(4);
}
Then we have
f(Math.sqrt);
2
f(function(z) { return z * (z + 1); });
20
What happens if we (oddly) ask the interpreter to evaluate the combination f(f);? Explain.
Higher-Order Functions
One of the things that makes JediScript different from other common programming languages is the ability to operate with higher-order functions, namely, functions that manipulate and generate other functions. The problem set will give you extensive practice with
higher-order functions and simple compound data types, in the context of a language for
graphing two-dimensional curves and other shapes.
In the problem set, we will be using many functions which may be applied to many different
types of arguments and may return different types of values. To keep track of this, it will
be helpful to have some simple notation to describe types of JediScript values.
Two basic types of values are Number and Boolean. Number are the JediScript numbers
such as 3, -4.2, 6.931479453e89. Boolean are the truth values true,false. The function
Math.sqrt may be applied to a Number and will return another Number. We indicate this
with the notation:
Math.sqrt : Number Number
We call the type of this function Num-Transform.
Num-Transform : Number Number
If f and g are functions of type Num-Transform, then we may compose them:
function compose(f, g){
return function(x){
return f(g(x));
};
}
As we have used it above, the function compose takes as arguments two functions of type
Num-Transform, and returns another such function. We indicate this with the notation:
compose : (Num-Transform, Num-Transform) Num-Transform
Just as squaring a number multiplies the number by itself, thrice of a function composes the function three times. That is, (thrice(f))(n) will return the same number as
f(f(f(n))):
function thrice(f) {
return compose(compose(f, f), f);
}
(thrice(Math.sqrt))(6561);
// Value: 3
Math.sqrt(Math.sqrt(Math.sqrt(6561)));
// Value: 3
As used above, thrice is of type Num-Transform Num-Transform, which, upon expansion, is:
(Number Number) (Number Number)
That is, it takes as input a function from numbers to numbers and returns the same kind
of function. But thrice will actually work for other kinds of input functions. It is enough
for the input function to have a type of the form T T , where T may be any type. So more
generally, we can write
thrice : (T T ) (T T )
Composition, like multiplication, may be iterated. Consider the following:
function identity(x) { return x; }
function repeated(f, n) {
if (n === 0) {
return identity;
} else {
return compose(f, repeated(f, n - 1));
}
}
(repeated(Math.sin, 5))(3.1);
// Value: 0.041532801333692235
Math.sin(Math.sin(Math.sin(Math.sin(Math.sin(3.1)))));
// Value: 0.041532801333692235