Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

F# List.length always return 1?

Tags:

list

f#

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?

like image 648
Hind Forsum Avatar asked Feb 18 '16 06:02

Hind Forsum


1 Answers

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]
like image 147
Random Dev Avatar answered Sep 17 '22 16:09

Random Dev