How do I create an operator in Haskell?

前端 未结 4 1601
栀梦
栀梦 2021-02-03 22:24

Making a ternary logic table, and I would like to make my own function for an operator that I\'ll call <=>.

So, for example, I want to do this, but

4条回答
  •  南笙
    南笙 (楼主)
    2021-02-03 22:36

    Since you have Eq and Ord, you can do the following:

    data Ternary = T | F | M
    deriving (Eq, Show, Ord)
    
    (<=>) :: Ternary -> Ternary -> Ternary
    x <=> y = if x == y then T else max x y
    

    If you do happen to change it so that M <=> M == M, then you can do the following:

    data Ternary = M | T | F
    deriving (Eq, Show, Ord, Enum)
    
    (<=>) :: Ternary -> Ternary -> Ternary
    x <=> y = fromEnum $ rem (toEnum x * toEnum y) 3
    

提交回复
热议问题