我有這個代碼:
oof [] = 1
oof [(i,j)] = i j
oof [(i,7),(5,j)] = i*j
oof (_:_:xs) = foo xs
我把它輸入到 ghci 中。當我嘗試執行運算式時:
oof [(3,7),(5,2)]
它應該使用第三行并回傳 6,但我會收到錯誤訊息:
Exception: <interactive>:11:1-21: Non-exhaustive patterns in function foo
為什么我會得到這個例外?
uj5u.com熱心網友回復:
您可能一次輸入一行。:{您應該在and之間包裝多行陳述句:},因此:
ghci> :{
ghci| oof [] = 1
ghci| oof [(i,j)] = i j
ghci| oof [(i,7),(5,j)] = i*j
ghci| oof (_:_:xs) = oof xs
ghci| :}
ghci> oof [(3,7),(5,2)]
6
如果您逐行輸入這些,則每次都定義一個新的更區域范圍的函式oof,該函式僅適用于最后定義的子句。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/495985.html
上一篇:無法匹配型別變數
