For an assignment for my functional programming class I am working on an exercise on trees. And more specifically on Rose trees. In the framework that we got the data type 'Rose' is already defined, but it has an operator :>. I searched it on hoogle and it said that it's the rightmost element of a sequence and the rest of the sequence. (Part of the framework below)
data Rose a = a :> [Rose a]
deriving (Eq, Show)
-- Exercise 1
root :: Rose a -> a
root = undefined
children :: Rose a -> [Rose a]
children = undefined
I by no means need you guys to tell me how the root and children function should be made. But if you guys could give me some tips as to how to read the 'data Rose a' line or maybe show me how a rose tree would be build up. These things would be a really big help as I enjoy doing the excercises.
I hope someone can point me in the right direction.
t's very similar to the definition of a list:
(:>)
is a bit like (:)
data Rose a = a :> [Rose a]
tells you, that you can get a t :: Rose a
x :: a
rs :: [Rose a]
t = x :> rs
of course you can get back the elements like this too:
root (x :> rs) = ...
I hope you get the rest by yourself ;)
The :>
operator is actually a data constructor. It would be equivalent to define the type as
data Rose a = Node a [Rose a]
Where (:>) = Node
. So with this alternate definition you would have
root :: Rose a -> a
root (Node a subnodes) = a
Substituting for the actual constructor :>
you would have
root ((:>) a subnodes) = a
Which can also be written as
root (a :> subnodes) = a
As @Carsten says it's just like the list constructor :
, just specialized for the Rose
data type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With