Why I get the output like below?
> List.length [1,2,3];;
val it : int = 1
> List.length [1,2,3,4];;
val it : int = 1
I expected to get 3 and 4! Am I using incorrect function call?
It's not about the function - it's about the way you enter the items of the list. You use ,
instead of ;
!
> List.length [1,2,3];;
val it : int = 1
> List.length [1;2;3];;
val it : int = 3
the reason is that [1,2,3]
is a list of tuples with just one item:
> [1,2,3];;
val it : (int * int * int) list = [(1, 2, 3)]
see the (..)
in the output - sadly you can enter tuples with out the (..)
and many fall for it
If you use ;
instead you get a list of ints with 3 elements:
> [1;2;3];;
val it : int list = [1; 2; 3]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With