In OCaml, when having to check for the emptiness of a list data structure lst
, I would like to use
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
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.