我最近看到人们在他们的SML / NJ计划中使用as
。我找到的最有用的参考是"as" keyword in OCaml。
虽然OCaml也属于ML编程语言系列,但它们是不同的。例如,在上一个答案中给出的示例程序中,
let rec compress = function
| a :: (b :: _ as t) -> if a = b then compress t else a :: compress t
| smaller -> smaller;;
我对SML / NJ的翻译是(如果我做错了,请纠正我)
fun compress (a :: (t as b :: _)) = if a = b then compress t else a :: compress t
| compress smaller = smaller
如您所见,模式(b :: _ as t)
在第二个代码段中的顺序与(t as b :: _)
不同。 (尽管如此,它们的用法几乎相同)
对于可能的答案,我希望它可以包含(1)在SML / NJ的官方文档,课程和书籍中的任何一个关键字as
的引用,并且"可能" (2)举例说明其用法。我希望这个问题可以帮助未来的用户看到as
。
答案 0 :(得分:5)
as
关键字是标准ML定义(' 97修订版)的一部分。见page 79, figure 22(突出我的):
这些在Haskell中被称为as-patterns,几乎任何其他语言都允许将标识符绑定到(子)模式,但名称的来源显然来自ML。
它所服务的目的是为模式或其一部分命名。例如,我们可以捕获2元组列表的整个头部,同时为元组的值指定相同的名称。
fun example1 (list : (int * string) list) =
case list of
(* `head` will be bound to the tuple value *)
(* `i` will be bound to the tuple's 1st element *)
(* `s` will be bound to the tuple's 2nd element *)
head as (i, s) :: tail => ()
| nil => ()
其他用法出现在记录模式中。请注意,初看起来可能会给人的印象是,[{1}}关键字现在位于该名称的右侧,但它不是(请参阅下面的组合示例):
as
这是一个组合示例,您可以在其中看到记录中的fun example2 (list : { foo: int * string } list) =
case list of
(* `f` will be found to the value of the `foo` field in the record. *)
{ foo as f } :: tail => ()
(* The above can also be expressed as follows *)
| { foo = f } :: tail => ()
| nil => ()
用法与其他地方的用法一致,即名称保留在as
关键字的左侧(此处为name是记录标签)。
as