“Pattern matching” of algebraic type data constructors

后端 未结 5 1779
天涯浪人
天涯浪人 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:49

    Another possible way:

    sameK x y = f x == f y
      where f (Alpha _)   = 0
            f (Beta _)    = 1
            f (Gamma _ _) = 2
            -- runtime error when Delta value encountered
    

    A runtime error is not ideal, but better than silently giving the wrong answer.

提交回复
热议问题