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. };

Here is a sample session using this function:

> gcd_list( [ 5 ] )
5
> gcd_list( [ 5, 1, 35, 40 ] )
1
> gcd_list( [ 5, 15, 35, 40 ] )
5
> gcd_list( [ ] )
'Cannot ask for the gcd of the empty list'

The function above uses continuation-passing style in such a way that, if a 1 is ever encountered in the list, then the gcd function will never be called because we immediately know that the gcd of all of the numbers in the list must be 1 without the need for any computation. What should the question marks on line 5 be replaced with to ensure the function behaves this way?

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

Note that returning 1 (on line 6) requires no work. Of course, 1 must also be the correct return value for this case.