We want to use the fp.reduce function to compute the average of a list of numbers, as in:

> average( [ 2, 4, 1, 5 ] ) 
3 

The following implementation will work as long as you replace the question marks on line 4 with the correct expression.

1.  var average = function (ns) {
2.        var p = fp.reduce (
3.                            function (x,y) {
4.                                ????????????????????????
5.                            },
6.                            ns,
7.                            [ 0 , 0 ]
8.                          );
9.        return fp.div(fp.hd(p),fp.hd(fp.tl(p)));
10. };

Which one of the following expressions must replace the question marks above?

fp.makeList(fp.add(fp.hd(x),y),fp.add(fp.hd(fp.tl(x)),1));
  • fp.makeList(fp.add(x,fp.hd(y)),fp.add(1,fp.hd(fp.tl(y))));
  • fp.makeList(fp.add(1,fp.hd(fp.tl(y))),fp.add(x,fp.hd(y)));
  • fp.makeList(fp.add(1,fp.hd(fp.tl(y))),fp.add(fp.hd(x),y));
  • None of the other expressions will work.

What is the formula for computing the average of a list of numbers?

What is the semantics of the two arguments of the anonymous function in the code above?

What is the structure of the accumulator in this case?