Search code examples
smlsmlnj

Better Understanding Errors


I am a beginner in SML. It's something easy but it took like hours and yet I cant solve it.

I have to take a list of string and invert every string as well as list. and do everything without defining multiple functions outside the main. e.g. ["stack","overflow","nice"] would give ["ecin","wolfrevo","kcats"]

fun oppositelist(l)=foldl (op::) nil (map ((fn(x)=>(implode o rev o explode)(x)), l));

and the error message is:

Error: operator and operand don't agree [tycon mismatch]
  operator domain: 'Z -> 'Y
  operand:         (string -> string) * 'X
  in expression:
    map ((fn x => <exp> <exp>),l)

Don't solve everything, if the method is wrong. Hint it. Its a homework question.

Thank You.


Solution

  • The SML function map is curried. To map a function f over every element of the list l, you must write map f l. What you wrote is map (f,l), which means "map the function (f,l) over... something to be determined later." The compiler is complaining at you because (f,l) is not a function ('Z -> 'Y); it's a tuple consisting of a function and a list ((string -> string) * 'X).

    I haven't looked any closer at your solution, but if you comment in response to this answer and ask me to, I probably will. :)

    EDIT: Oh, and (fn(x)=>(implode o rev o explode)(x)) is just a long-winded way of writing (implode o rev o explode), for the same reason that writing (fn(x)=>f(x)) is a long-winded way of writing f.