标准ML-学习教程,但示例失败



我在这里学习教程。。。

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

连接运算符连接strings(其类型为string * string -> string),但firstsecond。。。函数的类型为CCD_ 5。显然,如果我们试图给^一个char * char元组,我们会收到编译器的抱怨。

教程链接到此页面并修复:

将问题6中给出的定义更改为
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中不工作,尽管他们的注释只适用于Moscow ML.

错误是说运算符^处理这样的字符串:

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

但是,函数firstsecondthirdfourthlast都返回字符。

字符串标准库中的函数implode在这里会有所帮助。

最新更新