Search code examples
smlsmlnj

Why is there type mismatch of operator and operand?


Why is there a tycon mismatch operator and operand do not agree error? Any Suggestion for a solution?

fun reve (x:string) = implode o rev o explode x

Solution

  • The problem lies in, that function application binds stronger than o.

    That is, it is interpreted as:

    fun reve x = implode o rev o (explode x);
    

    Since explode x is a char list and not a function, this will fail.

    You can fix this by placing your parentheses properly

    fun reve x = (implode o rev o explode) x;
    

    Or writing it in point-free notation:

    val reve = implode o rev o explode
    

    It's also possible to define a right-associative function application operator, usually called $, which does what you want without parentheses:

    (* Right-associative function application *)
    infixr $
    fun f $ x = f x;
    
    fun reve x = implode o rev o explode $ x