30

制作一个三元逻辑表,我想为我将调用的运算符制作自己的函数<=>

因此,例如,我想这样做,但这是不对的。这样做的正确方法是什么?

data Ternary = T | F | M
deriving (Eq,  Show, Ord)

<=> :: Ternary -> Ternary -> Ternary
<=> T F = F
<=> T T = T
<=> T M = M
<=> F F = T
<=> F T = F
<=> F M = M
<=> M F = M
<=> M T = M
<=> M M = T
4

4 回答 4

49

只需在您的运算符周围添加括号:

(<=>) :: Ternary -> Ternary -> Ternary
(<=>) T F = F
(<=>) T T = T
(<=>) T M = M
(<=>) F F = T
(<=>) F T = F
(<=>) F M = M
(<=>) M F = M
(<=>) M T = M
(<=>) M M = T

这将它从中缀形式变为前缀形式。或者,您可以只在定义中使用中缀:

(<=>) :: Ternary -> Ternary -> Ternary
T <=> F = F
T <=> T = T
T <=> M = M
F <=> F = T
F <=> T = F
F <=> M = M
M <=> F = M
M <=> T = M
M <=> M = T
于 2012-02-20T05:10:22.243 回答
12

带符号的函数名与不带符号的函数名有不同的语法:

-- Works:
(<^>) :: Int -> Int -> Int
a <^> b = a + b

-- Doesn't work:
{-
<^> :: Int -> Int -> Int
<^> a b = a + b
-}

-- Works:
letters :: Int -> Int -> Int
letters a b = a + b

-- Doesn't work:
{-
(letters) :: Int -> Int -> Int
a letters b = a + b
-}

不过,我保证 - Haskell 非常值得学习复杂的规则。

于 2012-02-20T06:01:04.373 回答
1

您可以按如下方式简化(逐行)定义:

(<=>) :: Ternary -> Ternary -> Ternary
T <=> T = T
F <=> F = T
M <=> M = T
M <=> _ = M
_ <=> M = M
_ <=> _ = F
于 2012-02-20T18:33:28.697 回答
0

由于您拥有Eqand Ord,您可以执行以下操作:

data Ternary = T | F | M
deriving (Eq, Show, Ord)

(<=>) :: Ternary -> Ternary -> Ternary
x <=> y = if x == y then T else max x y

如果您碰巧更改了它M <=> M == M,那么您可以执行以下操作:

data Ternary = M | T | F
deriving (Eq, Show, Ord, Enum)

(<=>) :: Ternary -> Ternary -> Ternary
x <=> y = fromEnum $ rem (toEnum x * toEnum y) 3
于 2012-02-24T02:36:59.017 回答