13

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)

kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
Joan Venge
  • 315,713
  • 212
  • 479
  • 689

4 Answers4

17

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
  • 733,204
  • 149
  • 1,241
  • 1,454
  • Thanks Jared, then the reason , is used for tuples is because defining literal tuples is more common than defining literal lists, right? – Joan Venge Sep 20 '10 at 17:54
  • 3
    @Joan I don't know the history of this choice other than it's a hold over from OCaml. – JaredPar Sep 20 '10 at 17:55
  • 1
    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 Sep 20 '10 at 18:07
  • Thanks Brian, can you please tell me what parameter lists are? Lists like [1;2;3]? – Joan Venge Sep 20 '10 at 18:14
  • 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 Sep 20 '10 at 18:58
  • 3
    Note that if F# had tuple syntax like Python where tuples have parens around them, you could have commas between list items. – Gabe Sep 20 '10 at 19:05
  • 2
    @Gabe: It does; there is parens around them. I have always wondered about the point that you made. – Muhammad Alkarouri Sep 20 '10 at 19:30
15

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
  • 117,631
  • 17
  • 236
  • 300
8

[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]

Gabe
  • 84,912
  • 12
  • 139
  • 238
Snark
  • 1,664
  • 14
  • 27
  • Thanks, why is there perf penalty for [1..9]? Isn't the compiler just unfolds it to [1;2;3...9]? – Joan Venge Sep 20 '10 at 17:55
  • 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 problems Sequences on the other hand worked fine. – Snark Sep 20 '10 at 17:57
  • 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 Sep 20 '10 at 18:13
  • 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 Sep 20 '10 at 18:18
  • 1
    sorry about the formatting, that should get the point across, as above you can use new lines to delimit a list – Snark Sep 20 '10 at 18:50
5

Just wondering the idea behind using ;?

Tuples are more common in ML and using ; lets you write lists of tuples as:

[1, 2; 3, 4]

Historically, F# inherited this syntax from OCaml which created its syntax by trying to remove more superfluous parentheses from Standard ML syntax.

J D
  • 48,105
  • 13
  • 171
  • 274