[Haskell-beginners] Rose tree
mike h
mike_k_houghton at yahoo.co.uk
Mon Jan 9 21:06:35 UTC 2017
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 <mike_k_houghton at yahoo.co.uk> 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
More information about the Beginners
mailing list