Safe Haskell | None |
---|---|
Language | Haskell2010 |
Data.Forest
Description
Multi-way trees (also known as rose trees) and forests, similar to Data.Tree
from the popular containers library.
Importing
Recommended imports:
import Data.Forest (Forest, Tree) import qualified Data.Forest as Forest
Types
Constructing
Deconstructing
Forest functor
One notable difference of this Forest
from that of the containers library is
that this Forest
is a newtype rather than a type alias, and so it provides a
more appropriate Functor
instance:
>>>
:{ example :: Forest Char example = forest [ tree 'a' $ leaves "bc" , tree 'd' $ forest [ leaf 'e' , tree 'f' $ leaves "g" ] ] :}
>>>
:{ printCharForest = putStrLn . showForest where showForest f = intercalate ", " (fmap showTree (trees f)) showTree t = case trees (subforest t) of [] -> [root t] [t'] -> [root t] <> ": " <> showTree t' ts -> [root t] <> ": (" <> showForest (subforest t) <> ")" :}
>>>
printCharForest example
a: (b, c), d: (e, f: g)
>>>
printCharForest (fmap toUpper example)
A: (B, C), D: (E, F: G)
Likewise, Forest'
s Foldable
instance folds over the elements of the forest.
>>>
toList example
"abcdefg"