I am trying to write a function that finds whether or not a given number n
is a perfect square. Here's my attempt:
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
Now, when I try to run this with sml myfile.sml
, I get the following error:
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
This doesn't seem to be a redundant pattern to me, because it only matches two constants and then anything else. Why does the compiler see this as redundant?