how to get a sub list from a list in ocaml

Posted by romerun on Stack Overflow See other posts from Stack Overflow or by romerun
Published on 2010-04-25T22:31:39Z Indexed on 2010/04/25 22:33 UTC
Read the original article Hit count: 181

Filed under:

Hi, I'm looking at the List document. It seems the library does not provide a sublist function.

I'm trying to get list of elements from i to j. Now I have to write it as:

let rec sublist list i j = if i > j then [] else (List.nth list i) :: (sublist list (i+1) j)

which is quite concise but I'm questioning the efficiency of List.nth, because if it's O(n), I would rather have to write it in a less concise way.

I'm wondering why didn't they provide List.sublist func, if List.nth is not O(1), because it's such a quite common operation..

© Stack Overflow or respective owner

Related posts about ocaml