Pergunta

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?

Foi útil?

Solução

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.)

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top