Implement terminals

This commit is contained in:
2022-03-14 20:39:23 +00:00
parent 1b6f11709c
commit f16f32d9d3
30 changed files with 313 additions and 119 deletions
+18
View File
@@ -9,6 +9,8 @@ the nodes in the trunk are [a,b] (note that d is not the first child of b).
-}
module Dodge.Tree.Polymorphic
( applyToRoot
, applyToSubtree
, applyToSubforest
, treeFromPost
, treeFromTrunk
, splitTrunk
@@ -56,6 +58,22 @@ applyToNode (i:is) f (Node x xs) = Node x (ys ++ [applyToNode is f z] ++ zs)
where
(ys, z:zs) = splitAt i xs
{- | Applies a function to a specific subtree determined by a list of indices.
Unsafe (partial function). -}
applyToSubtree :: [Int] -> (Tree a -> Tree a) -> Tree a -> Tree a
applyToSubtree [] f t = f t
applyToSubtree (i:is) f (Node x xs) = Node x (ys ++ [applyToSubtree is f z] ++ zs)
where
(ys, z:zs) = splitAt i xs
{- | Applies a function to a specific subforest determined by a list of indices.
Unsafe (partial function). -}
applyToSubforest :: [Int] -> ([Tree a] -> [Tree a]) -> Tree a -> Tree a
applyToSubforest [] f (Node p cs) = Node p (f cs)
applyToSubforest (i:is) f (Node x xs) = Node x (ys ++ [applyToSubforest is f z] ++ zs)
where
(ys, z:zs) = splitAt i xs
-- do not delete: find use for
--{- |
--Applies a function to the first node along a trunk that satisfies a given property.