tags:

views:

189

answers:

3

This might be a strange question but if I want to define a list of integers from:

1, 2, 3, 4, 5, 6, 7, 8, 9

Do I need to do it using the ; character?

[ 1; 2; 3; 4; 5; 6; 7; 8; 9 ]

instead of?:

[ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]

It just seems to me , is more natural and easy on the eyes. Just wondering the idea behind using ;? (Not criticizing)

+8  A: 

Yes you must. The reason why is that in F# the comma operator is used for tuple expressions. So the expression 1,2,...9 is interpreted as a single tuple with 9 values.

[1,2] // List with 1 element that is a tuple value - (int*int) list
[1;2] // List with 2 elements that are integer values - int list
JaredPar
Thanks Jared, then the reason , is used for tuples is because defining literal tuples is more common than defining literal lists, right?
Joan Venge
@Joan I don't know the history of this choice other than it's a hold over from OCaml.
JaredPar
Thanks Jared. I got it now.
Joan Venge
Note that in OCaml, calling `f(x,y)` means calling `f` with a single argument that is a tuple `(x,y)`. Parameter lists probably are more common that list literals, if one wants to posit the 'most common should use commas' rationale.
Brian
Thanks Brian, can you please tell me what parameter lists are? Lists like [1;2;3]?
Joan Venge
I think Brian meant by parameter lists the list of parameters (arguments) passed to a function. They are called list in almost all languages so you call a function `f` of two parameters using the syntax `f(x,y)`. However in OCaml and F# such a call is passing a tuple to a function of a single parameter. This makes the use of tuples more common. In short, tuples (used as parameters) are more common than list literals in F#/OCaml.
Muhammad Alkarouri
Note that if F# had tuple syntax like Python where tuples have parens around them, you could have commas between list items.
Gabe
@Gabe: It does; there is parens around them. I have always wondered about the point that you made.
Muhammad Alkarouri
+6  A: 

[1,2,3,4,5] is a list of 1 element of type int * int * int * int * int

[1;2;3;4;5] is a list of 5 elements of type int

also, list comprehensions and ranges are your friends

let bar = [1..9], 1..9 is a range so it get's unfolded into 1;2;3;...9;

let blort = [for i in 1..9 -> i] is a comprehension that does the same thing -- a little more power for some performance issues.

Edit: for completeness sake, you can also do

let foo = [1
           2
           3]

and get a list of [1;2;3]

Thanks, why is there perf penalty for [1..9]? Isn't the compiler just unfolds it to [1;2;3...9]?
Joan Venge
in the case of [1..9] there shouldn't be, but list comprehensions can get quite complicated i.e. [for i in 1 .. 9 -> i * i], I have tried to use nested for loops in list comprehensions and had some substantial problemsSequences on the other hand worked fine.
Thanks, by "substantial problems" you mean perf problems? Also so [1..9] unfolds at compile time but [for i in 1 .. 9 -> i * i] is unfolded at runtime? Or are they all executed at runtime?
Joan Venge
Oh wow, thanks. Can you really do [1 2 3] to get [1;2;3]? Then it's even better. Way better. Thanks again.
Joan Venge
sorry about the formatting, that should get the point across, as above you can use new lines to delimit a list
+8  A: 

Other answers have pointed out the main reason.

As an aside it is worth noting that, like most other places that semicolon is used in the language, an alternative is a newline. For example

[ "foo"; "bar"; "baz" ]

can also be written as

[ "foo"
  "bar"
  "baz" ]

or a variety of other layouts where semicolons are replaced by newlines.

Brian
Thanks the newline trick is useful.
Joan Venge