I have this simple list structure where every leaf holds two values (a and b) and every node holds one value (a)
data List a b = Leaf (a, b) | Node (a, (List a b)) deriving Show
And I have this function just returning the value of a node or the first value of a leaf
func (Leaf (a, b)) = a
func (Node (a, c)) = a
Is there a way avoid one of these pattern matches? In fact I'm working on a ternary-tree and functions which have more than one argument, so I have a lot of pattern matches which are all doing the same thing.
I thought about something like this
func (var (a, b)) = a
where var can be a leaf or a node, but this does not work.
(a, b)can be thought of as one value, actually. And the answer is, I think, no, or at least no, unless you're fine with using SYB.List a b = Leaf a b | Node a (List a b)and then define the two patterns astop (Leaf a _) = aandtop (Node a _) = a.