/* Higher-order programming in pizza Some of the material here we've seen in Scheme, the principal difference is that pizza does it in a TYPED setting. */ public class deepdish { // polymorphic function that maps a function onto an array (destructively) static void map(T[] A, (T)->T f) { for(int i=0;i T fold(T[] A, (T,T)->T f, T id) { int i = 0; T ax = id; // accumulator initialized to identity for(i=0;iint accumulator(int val) { return fun (int x)->int { val += x; return val; }; } // Curried function. This function takes a function that takes two arguments // and a value for the first argument. It returns a function that expects // the second argument: static (B)->C prebind( (A,B)->C f, A val) { (B)->C curried = fun (B x)->C { return f(val,x); }; return curried; } // tail-recursive factorial function - for testing prebind public static int ifact(int ax, int n) { if (n<2) return ax; else return ifact(ax*n,n-1); } public static void main(String[] args) { int i = 0; (int)->int f; f = fun (int x)->int { return x*x; }; // inlined function int A[] = {2,4,6,8}; map(A,f); for(i=0;iString {return s+s;}); for(i=0;iint {return a+b;},0); System.out.println("sum is "+x); ///// testing prebind: (int)->int factorial = prebind(ifact,1); System.out.println("5! is "+factorial(5)); // FYI: "prebind" will be a built-in operation in Perl version 6, and // can bind parameters other than the first one. } // main } // deepdish