我有一段时间没有使用 SML 了,我遇到了这行代码:
type memory = string -> int;
这是否将“内存”定义为一个函数,它接受一个字符串 a 返回一个 int,或者完全是其他的东西?我搜索了一个类似的声明,但我似乎找不到或弄清楚它的作用。
当我把它放入 SML/NJ 时,我得到了这个:
- type memory = string -> int;
type memory = string -> int
memory
不是一个函数,它只是一个类型的缩写,它是一个将字符串作为输入并返回一个 int 的函数。所以每当你想写一些东西是类型的,string->int
你可以写它是类型的memory
。
例如,而不是写:
- fun foo(f : string->int, s) = f s;
val foo = fn : (string -> int) * string -> int
你可以写:
- fun foo( f: memory, s) = f s;
val foo = fn : memory * string -> int
这样的声明可以使您的代码更具可读性(例如,您可以创建一个缩写词,然后您可以编写这样的类型,type
而不是写出一对x
是int*int
类似的类型)。(x: int*int)
type pair = int*int
x
pair
(x: pair)