“Pattern matching” of algebraic type data constructors

后端 未结 5 1774
天涯浪人
天涯浪人 2021-02-07 15:07

Let\'s consider a data type with many constructors:

data T = Alpha Int | Beta Int | Gamma Int Int | Delta Int

I want to write a function to che

5条回答
  •  孤城傲影
    2021-02-07 15:51

    You can definitely use the generics to eliminate the boilerplate. Your code is a textbook example why I (and many others never use the _ wildcard at top level). While it is tedious to write out all the cases, it is less tedious than dealing with the bugs.

    In this happy example I would not only use Dave Hinton's solution but would slap an INLINE pragma on the auxiliary function f.

提交回复
热议问题