18 Combine

When we wrote map, we transformed each element of the list. What if we wanted to compile all the elements of a list together? We could sum or concatenate all the elements in a list:

Now we have a bunch of repeated code! Let's extract the repeated code into a function!

Now let's rewrite sum and concat in terms of combine.

Now we've factored out all the common code!

The code that we just wrote is very similar to an OCaml standard library function family called fold.