Rose tree

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Rose tree

Mike Houghton
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
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Rose tree

Mike Houghton

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 <[hidden email]> 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

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners