Search code examples
sml

Standard ML - Going through a tutorial but example fails


I'm going through the tutorial here...

http://www.soc.napier.ac.uk/course-notes/sml/tut2.htm

where it defines several things to be used as a function...

val first = hd o explode;
val second = hd o tl o explode;
val third = hd o tl o tl o explode;
val fourth = hd o tl o tl o tl o explode;
val last = hd o rev o explode;

Which then is used...

fun roll s = fourth s ^ first s ^ second s ^ third s;

It seems like it should work but when I try that, I get the following errors below. Anyone know what might be going?

stdIn:159.14-159.53 Error: operator and operand don't agree [tycon mismatch]
  operator domain: string * string
  operand:         char * char
  in expression:
    fourth s ^ first s

stdIn:159.14-159.53 Error: operator and operand don't agree [tycon mismatch]
  operator domain: string * string
  operand:         _ * char
  in expression:
    fourth s ^ first s ^ second s

stdIn:159.14-159.53 Error: operator and operand don't agree [tycon mismatch]
  operator domain: string * string
  operand:         _ * char
  in expression:
    fourth s ^ first s ^ second s ^ third s

Solution

  • The concatenation operator concatenates strings (it's type is string * string -> string), but the first, second, ... functions are of type string -> char. Obviously we're going to get a complaint from the compiler if we try to give ^ a char * char tuple.

    The tutorial links to this page with a fix:

    Change the definitions given in question 6 to
    fun roll s = implode[fourth s,first s,second s,third s];
    fun exch s = implode[second s,first s,third s,fourth s];

    I'd imagine their code works in some version of SML, but it doesn't work in SML/NJ for me, despite the fact their note is only for Moscow ML.