In Harper's chapter "Higher-Order Functions => Returning Functions", he gives 3 different reduce functions and discusses my each subsequent function is superior to the first and most intuitive function.

- time (fn x => reduce (1, (op +), g) ); 
Done: 1.208 
- time (fn x => better_reduce (1, (op +), g) ); 
Done: 1.138 
- time (fn x => staged_reduce (1, (op +)) g ); 
Done: 1.079 

I had to  open IntInf  to get this to work because the list was huge and the additions were even larger. But I have a big question. If thunk takes no arguments then how is it successfully called when the expression  fn x => better_reduce (1, (op +), g) )  creates a "thunk" which expects one argument,  x  ?

fun reduce (unit, opn, nil)  =    unit 
  | reduce (unit, opn, h::t) = 
    opn (h, reduce (unit, opn, t)) ; 
 
fun better_reduce (unit, opn, l) = 
    let 
        fun red nil = unit 
          | red (h::t) = opn (h, red t) 
    in 
        red l 
    end 
 
fun staged_reduce (unit, opn) = 
    let 
        fun red nil = unit 
          | red (h::t) = opn (h, red t) 
    in 
        red 
    end 
 

BattleOfTheReduceFunctions (last edited 2008-07-09 05:47:55 by localhost)