gpt4 book ai didi

sml - 令人困惑的类型声明?

转载 作者:行者123 更新时间:2023-12-02 07:10:57 24 4
gpt4 key购买 nike

我有一段时间没有使用 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),你可以只创建一个缩写 type pair = int*int 然后你可以写 x 是类型像这样(x: pair)).

关于sml - 令人困惑的类型声明?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5954042/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com