“as” keyword in OCaml

六月ゝ 毕业季﹏ 提交于 2019-12-21 03:59:08

问题


In the answers for the tutorials for OCaml available at this site, some of the solutions, including the one for eliminating consecutive duplicates of list elements, is written as such:

let rec compress = function
    | a :: (b :: _ as t) -> if a = b then compress t else a :: compress t
    | smaller -> smaller;;

What is the relevance of the line a :: (b:: _ as t)? Why can't I write it as a :: b :: t instead?


回答1:


The t in b :: _ as t is bound to b :: _. So the meaning is different. If you use the pattern a :: b :: t you would need to say compress (b :: t), which is a little less elegant and a tiny bit less efficient.




回答2:


The keyword as binds a name to all or part of a pattern. Once bound, the name can be used instead of the pattern it represents. In your "compress" function, t is bound to the pattern b :: _. Once t is bound, it can be used in subsequent expressions, as in the rest of the "compress" function.

as name binding occurs left-to-right, unlike most languages (excepting C's typedef). Also, :: appears to have higher precedence than as.

Therefore, (b :: _ as t) is equivalent to ((b :: _) as t). This can be confusing for those used to right-to-left bindings. Note that a :: (b :: _) as t will bind the whole pattern a :: b :: _ to t, due to the precedence mentioned above.

Reference:

  • http://caml.inria.fr/pub/docs/manual-ocaml/patterns.html#hevea_manual.kwd7
  • http://caml.inria.fr/pub/docs/oreilly-book/html/book-ora016.html.


来源:https://stackoverflow.com/questions/26769403/as-keyword-in-ocaml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!