Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pattern matching on variant without deconstructing in Haskell

In the following code, the function disp is defined by de-constructing Sum b c and then immediately reconstructing it. The problem is that I don't need b and c, only the fact that it is of type Sum.

data Expr = Name String | Sum Expr Expr
    deriving(Show)

disp (Sum (Name a) (Sum b c)) = a ++ ":" ++ disp (Sum b c)

Is there a way of writing disp without this deconstruction - reconstruction (and the b and c bindings), or is this the correct way of writing such a function?

like image 657
fouronnes Avatar asked Dec 02 '25 23:12

fouronnes


1 Answers

disp (Sum (Name a) s@(Sum _ _)) = a ++ ":" ++ disp s

@ allows multiple matches for the same thing

like image 56
Fraser Avatar answered Dec 04 '25 22:12

Fraser