Arc Forumnew | comments | leaders | submitlogin
1 point by rocketnia 4926 days ago | link | parent

I think it's cleaner to define 'reduce in terms of 'foldl:

  (def reduce (func xs)
    (whenlet (x . xs) xs
      (foldl func x xs)))
But I don't even really want 'reduce. If the function is '+, an empty list should give 0. If the function is '*, an empty list should give 1. Instead of trying to divine this value from nowhere, I think it makes more sense to pass the base case manually, as with 'foldl.

Alternatively we could have this...

  (def reduce (func xs)
    (iflet (x . xs) xs
      (foldl func x xs)
      (func)))
...but if the function is set up to take different numbers of arguments, we probably should have done (apply func xs) in the first place.