diff --git a/src/Dodge/DoubleTree.hs b/src/Dodge/DoubleTree.hs new file mode 100644 index 000000000..8758106f9 --- /dev/null +++ b/src/Dodge/DoubleTree.hs @@ -0,0 +1,26 @@ +module Dodge.DoubleTree where + +import Dodge.Data.DoubleTree + +-- conceptually, in a tree growing from left to right, +-- bottom -> top is equated with left -> right. +-- this does not match with thinking of a list as top -> bottom, so take care +doubleTreeToIndentList :: DoubleTree a -> [(a,Int,DoubleTreeNodeType)] +doubleTreeToIndentList = dtIL DTRootNode + +dtIL :: DoubleTreeNodeType -> DoubleTree a -> [(a,Int,DoubleTreeNodeType)] +dtIL nt (DT x l r) = map doindent (concat (headMap (dtIL DTBottomNode) (dtIL DTMidBelowNode) l)) + ++ [(x,0,nt)] + ++ map doindent (concat (lastMap (dtIL DTTopNode) (dtIL DTMidAboveNode) r)) + where + doindent (a,b,c) = (a,b+1,c) + +headMap :: (a -> b) -> (a -> b) -> [a] -> [b] +headMap f g (x:xs) = f x : map g xs +headMap _ _ [] = [] + +lastMap :: (a -> b) -> (a -> b) -> [a] -> [b] +lastMap _ _ [] = [] +lastMap f _ (x:[]) = [f x] +lastMap f g (x:xs) = g x : lastMap f g xs +