简体   繁体   中英

Ocaml: using list.length

I am trying to find the length of a list in ocaml.

I call a function first called

 > let get_list_length e in > print_list_length out x 

the actual code in get_list_length is where I am confused. The "e" is a list of "commands" and I want to find the length of all the "commands".

let get_list_length(e:values) : unit =
match e with
let x = list.length(e);;

So my e is the list of "commands" which are a bunch of values specified in my grammar file. I am confused about how to get the length of the list since e is a list of values and I want the length of that list.

Any help would be appreciated.

To get the length of a list is simple:

List.length my_list

Your get_list_length function can be as simple as:

let get_list_length e = List.length e

or more simply:

let get_list_length = List.length

As you currently have defined it, get_list_length returns type unit so you won't get anything useful from it. You are also using match incorrectly, it's usually used like this:

match e with something -> do something
| something_else -> do something_else

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.

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