
Hi, I’m rapidly descending into a spiral of confusion! :) How do I add something to a rose tree? I have data Tree a = Node a [Tree a] deriving (Show) add :: Eq a => a -> a -> Tree a -> Tree a and the first ‘a’ is the thing to add and the second is its parent node. i.e. add :: Eq a => a -> a -> Tree a -> Tree a add x px (Node x' []) = Node x' [Node x []] ….. I’ve tried various ways for the more general case but always seem to lose part of the tree. Any advice would be very welcome. Thanks Mike

add :: Eq a => a -> a -> RoseTree a -> RoseTree a add x px (Node x' []) = Node x' [Node x []] add x px (Node x' trees) | px == x' = Node x' $ (Node x []):trees | otherwise = Node x' $ (add x px) <$> trees seems to be correct. :)
On 8 Jan 2017, at 20:06, mike h
wrote: Hi,
I’m rapidly descending into a spiral of confusion! :)
How do I add something to a rose tree?
I have
data Tree a = Node a [Tree a] deriving (Show)
add :: Eq a => a -> a -> Tree a -> Tree a
and the first ‘a’ is the thing to add and the second is its parent node. i.e. add :: Eq a => a -> a -> Tree a -> Tree a add x px (Node x' []) = Node x' [Node x []] …..
I’ve tried various ways for the more general case but always seem to lose part of the tree.
Any advice would be very welcome.
Thanks
Mike
participants (1)
-
mike h