简体   繁体   English

OCaml中此空列表检查(list = [])的任何副作用?

[英]Any side effects of this empty list checking (list = []) in OCaml?

In OCaml, when having to check for the emptiness of a list data structure lst , I would like to use 在OCaml中,有检查清单数据结构的空虚时lst ,我想用

if (lst = []) then ...

for its conciseness, instead of using pattern matching or checking length of the list. 为了简洁,而不是使用模式匹配或检查列表的长度。

I wonder if there is any side effect of this structural comparison (=) over the list data structure? 我想知道这个结构比较(=)是否会对列表数据结构产生任何副作用?

There are no side effects. 没有副作用。 It's a perfectly good way to test whether a list is empty. 这是测试列表是否为空的完美好方法。

Many times however, you'd like to work with some elements of the list if it's not empty. 但是,很多时候,如果它不是空的,你想要使用列表中的一些元素。 Pattern matching is good for those times. 模式匹配适用于那些时代。

As a tiny side comment, if you're interested in conciseness you don't need the parentheses in your if . 作为一个微小的评论,如果你对简洁感兴趣,你不需要你的if括号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM