标准 ML - 完成教程但示例失败

Standard ML - Going through a tutorial but example fails

我正在阅读这里的教程...

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

其中它定义了几个要用作函数的东西...

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;

然后使用...

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

它似乎应该可以工作,但是当我尝试这样做时,出现以下错误。有人知道会发生什么吗?

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

错误是说运算符 ^ 对这样的字符串起作用:

"ab" ^ "cd" == "abcd"

但是,函数 firstsecondthirdfourthlast 都是 return 个字符。

the string standard library 中的函数 implode 在这里会有所帮助。

连接运算符连接strings(它的类型是string * string -> string),但是firstsecond、...函数是[=14类型的=].显然,如果我们尝试给 ^ 一个 char * char 元组,我们会收到编译器的投诉。

教程链接到 this page 并修复:

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];

我想他们的代码在 一些 版本的 SML 中工作,但它在 SML/NJ 对我来说不起作用,尽管事实上他们的注释只是莫斯科 ML.