added my implementation of `fold` that uses pattern matching