Consider the following definition of the gcd_list function that, when completed, will compute the greatest common divisor of a list of numbers:

1.  var gcd_list = function (ns) {
2.      var gcd_list_cps = function (ns,k) {
3.           if ( ????? ) {
4.               return "Cannot ask for the gcd of the empty list";
5.           } else if ( ????? ) {
6.               return 1;
7.           } else if ( ????? ) {
8.               return k(fp.hd(ns));
9.           } else {
10.              return gcd_list_cps( fp.tl(ns),
11.                                   function (x) {
12.                                       return k(gcd( ????? ));
13.                                   });
14.          }
15.      };
16.      return gcd_list_cps(ns,function (x) { return x; });
17. };

The function above uses continuation-passing style. What should the question marks on line 12 be replaced with to ensure the function behaves this way?

fp.hd(ns), x
  • fp.isEq(fp.hd(ns),1)
  • fp.isNull(fp.tl(ns))
  • fp.isNull(ns)

Note that, in this case (lines 10-13), we make the recursive call to the helper function with two arguments, namely the tail of the input list and a new continuation.

The specific argument given to the continuation in the call on line 12 must be the result of computing one more gcd operation after having already computed other gcd operations (whose result is the value of x).