我讀過關于Haskell中這種警告的類似問題,但這些問題一般都會涉及到串列,而我的問題并不涉及:
teste :: Char -> Char -> Int teste a b
|ord (toUpper a) < ord (toUpper b) = 1
|ord (toUpper a) > ord (toUpper b) = 2 >。
|ord a > ord (toUpper b) = 3
|ord a < ord (toUpper b) = 4 >。
為什么會出現 "模式匹配 "的警告?
Pattern 匹配(es)是非窮舉性的。
In an equation for `teste': Pattern不匹配。_ _
有什么我可以做的嗎?
編輯:新函式的代碼:
alphabetOrder :: Char -> Char -> Char ->
alphabetOrder a b = case compare (toUpper a) (toUpper b) of LT -> a; GT -> b; EQ -> a
uj5u.com熱心網友回復:
當ord a == ord (toUpper b)的結果應該是什么?你還沒有說明,這確實是一種可能性。
但是,即使是這樣的結果,也是有可能的。
但是,即使你指定了這一點,編譯器仍然無法判斷>、<和==涵蓋所有可能的情況。對于編譯器來說,>、<和==只是一些函式,它無法證明其中一個函式將總是回傳True。
因此,你真正需要做的是添加一個案例,當沒有其他案例匹配時,它將被匹配:
teste :: Char -> Char -> Int teste a b
|ord (toUpper a) < ord (toUpper b) = 1
|ord (toUpper a) > ord (toUpper b) = 2 >。
|ord a > ord (toUpper b) = 3
|ord a < ord (toUpper b) = 4 >。
|否則=5
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/316949.html
標籤:
