Have a look into recursion schemes. There's a related question here that includes loads of links:
Recursion schemes for dummies?
All of the links on that question are excellent, but I'd particularly look at Tim Williams' slides (link in my answer to that question) for concrete implementations of the various different recursion patterns (most of which are demo'd on tree structures).