I'm pretty new to Haskell and still have some problems getting my head around functional programming. With that said:
I have a custom n-ary tree datatype
data Tree = Empty | Leaf String | Node String [Tree]
I'm trying to write a function to replace an element in a tree, i.e.
replaceInTree :: String -> String -> Tree -> Maybe Tree
Replacing the first string with the second. There is only ever one occurance of each string so I can stick with the first one found. I've made a few efforts but I can't grasp how to reconstruct the full tree after replacing the element. Trivially, I have this:
ntreeReplace x y (Node z lis)
|(x==z) = Just (Node y lis)
which only changes the head node
, obviously. I've written a function that returns true if an element is present in the tree, as a leaf
or node
, but progress beyond that is proving difficult.
Thanks for any help!