“作为" OCaml中的关键字 [英] "as" keyword in OCaml

查看:75
本文介绍了“作为" OCaml中的关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

本网站上可获得的有关OCaml教程的答案中,有一些解决方案,包括消除列表元素连续重复的解决方案,写成这样:

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;;

a :: (b:: _ as t)行的相关性是什么?为什么我不能将其写为a :: b :: t?

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

推荐答案

b :: _ as t中的t已绑定到b :: _.因此含义是不同的.如果使用模式a :: b :: t,则需要说compress (b :: t),它不太优雅,但效率稍低.

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.

这篇关于“作为" OCaml中的关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆