I have a set of problems that I've been working through and can't seem to understand what the last one is asking. Here is the first problem, and my solution to it:
a) Often we are interested in computing ∑i=m..n f(i), the sum of function values f(i) for i = m through n. Define
sigma f m nwhich computes ∑i=m..n f(i). This is different from definingsigma (f, m, n).
fun sigma f m n = if (m=n) then f(m) else (f(m) + sigma f (m+1) n);
The second problem, and my solution:
b) In the computation of sigma above, the index i goes from current i to next value i+1. We may want to compute the sum of f(i) where i goes from current i to the next, say i+2, not i+1. If we send this information as an argument, we can compute more generalized summation. Define ‘sum f next m n’ to compute such summation, where ‘next’ is a function to compute the next index value from the current index value. To get ‘sigma’ in (a), you send the successor function as ‘next’.
fun sum f next m n = if (m>=n) then f(m) else (f(m) + sum f (next) (next(m)) n);
And the third problem, with my attempt:
c) Generalizing sum in (b), we can compute not only summation but also product and other forms of accumulation. If we want to compute sum in (b), we send addition as an argument; if we want to compute the product of function values, we send multiplication as an argument for the same parameter. We also have to send the identity of the operator. Define ‘accum h v f next m n’ to compute such accumulation, where h is a two-variable function to do accumulation, and v is the base value for accumulation. If we send the multiplication function for h, 1 for v, and the successor function as ‘next’, this ‘accum’ computes ∏i=m..n f(i). Create examples whose ‘h’ is not addition or multiplication, too.
fun accum h v f next m n = if (m>=n) then f(m) else (h (f(m)) (accum (h) (v) (f) (next) (next(m)) n));
In problem C, I'm unsure of what i'm suppose to do with my "v" argument. Right now the function will take any interval of numbers m - n and apply any kind of operation to them. For example, I could call my function
accum mult (4?) double next3 1 5;
where double is a doubling function and next3 adds 3 to a given value. Any ideas on how i'm suppoes to utilize the v value?