I'm trying to write a function that combines a list of rose trees with the their parent node being the highest values of the root nodes of the given rose trees. For example;
RosesToRose [Rose 1 [Rose 1 [], Rose 2 []], Rose 3 [], Rose 4 [Rose 10 []]]
should return Rose 4 [Rose 1 [Rose 1 [], Rose 2 []], Rose 3 [], Rose 4 [Rose 10 []]]
I'm getting an error " Non-exhaustive patterns in function rosesToRose" and I'm not sure whats causing it. Tried matching against an empty list as input and got the same error. Any suggestions would be appreciated.
My code:
data Rose a = Rose a [Rose a]
deriving Show
rosesToRose:: (Ord a, Num a )=> [Rose a] -> Rose a
rosesToRose [(Rose node tree)] = Rose (maxRoseNode [(Rose node tree)]) [(Rose node tree)]
maxRoseNode:: (Ord a,Num a) =>[Rose a] -> a
maxRoseNode trs = case trs of
[] -> 0
(Rose node tree):xs -> maximum ([maxRoseNode xs] ++ [node])
The pattern you use will only match a list with exactly one element. Indeed the pattern:
matches a list with one
Rose
object. But in your sample data, you pass it a list with three elements. Furthermore themaxRoseNode
does not make much sense if you pass the same list with one element.You can solve the problem, by just matching this with a variable (
rs
for example), and then construct aRose
withmaxRoseNode rs
as value, andrs
as children:You can improve the readability (and efficiency) of
maxRoseNode
, by first checking if it is an empty list, and if not, calculate the maximum of the items wrapped in theRose
s:Here
\(Rose x _) -> x
is a lambda expression that will map aRose x _
object tox
.For example: