This commit is contained in:
2024-09-12 10:21:56 +01:00
parent 891fd0079c
commit 4dedba8d30
+26
View File
@@ -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