From 4dedba8d30ed5ee7e329d548e1330e17cf2d6ac9 Mon Sep 17 00:00:00 2001 From: justin Date: Thu, 12 Sep 2024 10:21:56 +0100 Subject: [PATCH] Add file --- src/Dodge/DoubleTree.hs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 src/Dodge/DoubleTree.hs 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 +