Reputation: 1229
If I have a list of floats:
let float_list = [1.5,2.5,3.5]
I'm looking to extract a number from this list similar to:
List.nth float_list 2
However what is returned is type float * float and not 3.5. List.nth only seems to return integers from an integer list and I can't seem to find a function in https://caml.inria.fr/pub/docs/manual-ocaml/libref/List.html to return the nth element of any list other than an integer list.
Would anyone be able to help with this?
Upvotes: 0
Views: 176
Reputation: 370425
Your float_list
isn't a list of floats, it's a list of float * float * float
tuples and it only contains one such tuple. So in addition to not having the type you want, List.nth float_list 2
will actually cause an exception at run time because 2
is not a valid index for that list.
You want [1.5; 2.5; 3.5]
, which is actually a list of floats. With that list List.nth
will work exactly as you want.
Upvotes: 3
Reputation: 29126
List literals in OCaml use ;
to separate items. ,
is used to separate items in tuples. So the type of float_list
is actually (float * float * float) list
, and it contains only a single element.
Upvotes: 1