implement foldl and foldr