Question

I have this function in ML:

fun repeat(x,n:int)=
        if n<=0 then []
        else x::repeat(x,n-1)
    |repeat (x,_)=[];   

now, I need the second repeat in case the second argument (n) will be something other than int. but I get a "match redundant" error for it. can someone explain please?

Was it helpful?

Solution

ML is a typed language. Your explicit type annotation determines that the second argument of the function has type int (which actually is already determined implicitly by its comparison to 0, so the annotation is redundant). So obviously, there cannot be anything other than an integer to match, and the type system knows it.

(Edit: Stylistic note: better throw an exception (e.g. the predefined Domain) in case that n < 0, rather than silently returning a random default result.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top