0
我想寫一個函數,查找給定的數字n
是否是一個完美的正方形。這裏是我的嘗試:冗餘模式匹配
local
fun perfect_square_iter x z = let val sqr = z * z in
case (x,z) of
(sqr,_) => true
| (_, 0) => false
| _ => perfect_square_iter x (z - 1)
end
in fun perfect_square n = perfect_square_iter n n
end
現在,當我嘗試使用sml myfile.sml
運行它,我得到以下錯誤:
lab03.sml:17.5-20.43 Error: match redundant
(sqr,_) => ...
--> (_,0) => ...
--> _ => ...
/usr/lib/smlnj/bin/sml: Fatal error -- Uncaught exception Error with 0
raised at ../compiler/FLINT/trans/translate.sml:1735.13-1735.21
這似乎不是一個多餘的模式對我來說,因爲它只匹配兩個常量,然後是其他任何東西。爲什麼編譯器認爲這是多餘的?