Parameterized fold, so that it can be used for foldl and foldr