I was thinking how can I generalize scanl to arbitrary scanl . Prelude's approach is to treat everything as a list (i.e., Foldable ) and apply scanl on a folded structure. Instead, I tend to think of scanl as an operation that passes state from each node of the tree to its children, using a monoidal op when it moves from root to leaves. So, for example, on Data.Tree we have:
scan :: (b -> a -> b) -> b -> Tree a -> Tree b scan fn init (Node element children) = Node (fn init element) $ map (treeScan fn (fn init element)) children
So for example:
main = do prettyPrint $ scan (+) 0 $ Node 1 [ Node 1 [ Node 1 [], Node 1 []], Node 1 [ Node 1 [], Node 1 []]]
Results in:
1 | +- 2 | | | +- 3 | | | `- 3 | `- 2 | +- 3 | `- 3
The same as using scanl through each tree path independently, preserving the original structure.
The question is quite simple: is this a meaningful generalization? Ie, is it usually used with a categorical explanation and possibly with a different name?
type-theory haskell category-theory
Maiavictor
source share