1
我感到有點糊塗了「匹配冗餘」的錯誤運行下面的代碼時,我得到:爲什麼我得到「匹配redudant」錯誤?
datatype expression = Constant of int |
Variable of string |
Operator of string * expression |
Pair of expression list |
List of expression list
fun add2 (ul1: expression, ul2: expression) =
let
fun gcd (a, b) =
if a >= b
then
if (a mod b) = 0
then b
else gcd (b,(a mod b))
else
if (b mod a) = 0
then a
else gcd (a, (b mod a))
fun lcm (a,b) =
a*b div (gcd(a,b))
in
case ul1 of
Operator("/",Pair [Constant a, Constant b]) =>
case ul2 of
Operator("/",Pair [Constant c, Constant d]) =>
a*d + c*b//(b*d)
|Operator("/",Pair [Variable c, Constant d])=>
Operator("/",Pair [(Operator("+", Pair [Constant a, Variable c])),Constant (lcm(b,d))])
|Operator("/",Pair [Variable a, Constant b]) =>
case ul2 of
Operator("/",Pair [Constant c, Constant d]) =>
Operator("/",Pair [(Operator("+", Pair [Variable a, Constant c])),Constant (lcm(b,d))])
|Operator("/",Pair [Variable c, Constant d])=>
Operator("/",Pair [(Operator("+", Pair [Variable a, Variable c])),Constant (lcm(b,d))])
end
確切的錯誤按摩:
C:\Users\erikj\Dropbox\Fakulteta Laptop\Programiranje\domacanaloga 6.sml:91.5-102.93 Error: match redundant and nonexhaustive
Operator ("/",Pair (Constant c :: Constant d :: nil)) => ...
Operator ("/",Pair (Variable c :: Constant d :: nil)) => ...
--> Operator ("/",Pair (Variable a :: Constant b :: nil)) => ...
我不介意關於非詳盡比賽因爲它只是一個練習。 非常感謝您的幫助!
最後兩種情況不是相同的嗎?我不知道SML,但看起來像構造函數匹配,最後2行使用'Variable'和'Constant'。 – Carcigenicate
最後兩行你確切的意思?內部「情況」在兩種情況下都與ul2相同,但您可以通過ul1的不同分支進行分析。我不明白爲什麼你會遇到冗餘問題,如果你有這樣的事情,因爲每個外部案例都應該有它自己的環境嗎?或者我只是在我的腦海裏錯誤地設置了一些東西。在這種情況下,這個例子有一個簡單的解決方法嗎? – Amuoeba