Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the reason of using ; in F# lists instead of ,?

Tags:

syntax

list

f#

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)

like image 601
Joan Venge Avatar asked Sep 20 '10 17:09

Joan Venge


People also ask

Is there any reason to use Fahrenheit?

Fahrenheit is superior for measuring temperature precisely. It's also better because humans tend to care more about air temperature rather than water temperature. For those reasons, we should welcome Fahrenheit as a standard of temperature measurement, rather than rejecting it for its metric counterpart.

Why does America use Fahrenheit instead of Celsius?

32 degrees and 212 degrees. In what world does this make sense?! Well, there was a time (a pretty brief time, but still) when using Fahrenheit made perfect sense, because it was the only way of measuring temperature. In the early 1700s, thermometers were rudimentary, few and far between, and imprecise.

Why did Fahrenheit choose 32 and 212?

After Fahrenheit's death in 1736, the Fahrenheit scale was recalibrated to make it slightly more accurate. The exact freezing and boiling points of plain water, minus the salt, were marked at 32 and 212 degrees Fahrenheit, respectively. Normal human body temperature was marked at 98.6.

What countries use Fahrenheit for temperature?

Today, countries that use the Fahrenheit include the Bahamas, Palau, Belize, the Cayman Islands, the Federated States of Micronesia, the Marshall Islands, and the United States and its territories such as Puerto Rico, the U.S. Virgin Islands, and Guam.


4 Answers

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

like image 153
Snark Avatar answered Oct 20 '22 04:10

Snark


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.

like image 39
Brian Avatar answered Oct 20 '22 05:10

Brian


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
like image 17
JaredPar Avatar answered Oct 20 '22 04:10

JaredPar


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.

like image 4
J D Avatar answered Oct 20 '22 05:10

J D