Assume the following mutually recursive structure:
type Tree<'a> =
| Empty
| Node of 'a * 'a Forest
and Forest<'a> =
| Nil
| Cons of 'a Tree * 'a Forest
Goal: Generate the common catamorphisms for this structure: foldl, foldr, foldk.
I have generated the naive-catamorphism as follows:
let rec foldTree fEmpty fNode fNil fCons =
function
| Empty -> fEmpty
| Node (a, f) -> fNode a (foldForest fEmpty fNode fNil fCons f)
and foldForest fEmpty fNode fNil fCons =
function
| Nil -> fNil
| Cons (t, f') -> fCons (foldTree fEmpty fNode fNil fCons t) (foldForest fEmpty fNode fNil fCons f')
How do I go about 'mechanically' generating the tail-recursive foldl (using accumulators) and tail-recursive foldr (using continuations)?
I have been through Scott's Recursive Types and Folds series and I understand how to generate the folds for a recursive structure 'mechanically'. However I cannot find anything on google to do the 'mechanical' thing for recursive data structures.
PS: One can get rid of the mutual-recursion above by in-lining but lets retain it as it represents a simplified version of the mutual recursion in tpetricek's Markdown parser.