Learning F# – Part 2

Dis­claimer: I do not claim cred­it for the code exam­ples and much of the con­tents here, these are most­ly extracts from the book by Chris Smith, Pro­gram­ming F#: A com­pre­hen­sive guide for writ­ing sim­ple code to solve com­plex prob­lems. In fact, if you’re think­ing of learn­ing F# and like what you read here, you should buy the book your­self, it’s easy to read and the author has gone go great lengths to keep things sim­ple and includ­ed a lot of code exam­ples for you to try out your­self.

Primitive Types

F# is sta­t­i­cal­ly typed, mean­ing that type check­ing is done at com­pile time.

F# sup­ports the full set of prim­i­tive .Net types which are built into the F# lan­guage and sep­a­rate from user-defined types.

Here’s a table of all the numer­ic types (both inte­ger and float­ing-point) with their suf­fix­es:


F# also allows you to spec­i­fy val­ues in hexa­dec­i­mal (base 16), octal (base 8 ) or bina­ry (base 2) using pre­fix 0x, 0o, or 0b:


There are no implic­it type con­ver­sion in F#, which elim­i­nates sub­tle bugs intro­duced by implic­it type con­ver­sion as can be found in oth­er lan­guages.

Arithmetic Operators

You can use stan­dard arith­metic oper­a­tors on numer­ic prim­i­tives, like oth­er CLR-based lan­guages, inte­ger divi­sion rounds down to the next low­est num­ber dis­card­ing the remain­der. Here’s a table of all sup­port­ed oper­a­tors:


A very impor­tant to note here is that by default, these arith­metic oper­a­tors do not check for over­flow! If a num­ber becomes too big for its type it’ll over­flow to be neg­a­tive, and vice ver­sa:


F# also fea­tures all the stan­dard math func­tions, here’s a table of the com­mon math func­tions:



If you are deal­ing with data larg­er than 2^64, F# has the Big­Int type for rep­re­sent­ing arbi­trar­i­ly large inte­gers. While the Big­Int type is sim­ply an alias for the System.Numerics.BigInteger type, it’s worth not­ing that nei­ther C# nor VB.Net has syn­tax to sup­port arbi­trar­i­ly large inte­gers.

Big­Int uses the I suf­fix for lit­er­als, see exam­ple below:


You should remem­ber that although Big­Int is heav­i­ly opti­mized, it is still much slow­er than using the prim­i­tive inte­ger types.

Bitwise Operations

Prim­i­tive inte­ger types sup­port bit­wise oper­a­tors for manip­u­lat­ing val­ues at a bina­ry lev­el:



The .Net plat­form is based on Uni­code, so char­ac­ters are rep­re­sent­ed using 2-byte UTF-16 char­ac­ters. To define a char­ac­ter val­ue, you can put any Uni­code char­ac­ter in sin­gle quotes, for exam­ple:


Like C#, to rep­re­sent spe­cial con­trol char­ac­ters you need to use an escape sequence from the table below:


You can get the byte val­ue of a char­ac­ter lit­er­al by adding a B suf­fix:



String lit­er­als are defined by enclos­ing a series of char­ac­ters in dou­ble quotes which can span mul­ti­ple lines. To access a char­ac­ter from with­in a string, use the index­er syn­tax, .[ ], and pass in a zero-based char­ac­ter index. For exam­ple:


If you want to spec­i­fy a long string, you can break it up across mul­ti­ple lines using a sin­gle back­slash, \, for exam­ple:


Like in C#, you can define a ver­ba­tim string using the @ sym­bol, which ignores any escape sequence char­ac­ters:


Boolean Values

F# has the bool type (System.Boolean) as well as stan­dard Boolean oper­a­tors list­ed below:


F# uses short-cir­cuit eval­u­a­tion when eval­u­at­ing Boolean expres­sions, mean­ing that if a result can be deter­mined after eval­u­at­ing the first of the two expres­sions, the sec­ond val­ue won’t be eval­u­at­ed. For exam­ple:

true || f() - will eval­u­ate to true with­out exe­cut­ing func­tion f.

false && g() — will eval­u­ate to false with­out exe­cut­ing func­tion g.

Comparison and Equality

You can com­pare numer­ic val­ues using stan­dard oper­a­tors list­ed below:


All these oper­a­tors eval­u­ate to a Boolean val­ue except the com­pare func­tion which returns -1, 0, or 1 depend­ing on whether the first para­me­ter is less than, equal to, or greater than the sec­ond.

You should have noticed that these oper­a­tors are sim­i­lar to those found in SQL Serv­er and F# doesn’t dis­tin­guish assign­ment from equal­i­ty (like C#, where = is assign­ment and == is equal­i­ty com­par­i­son).

When it comes to equal­i­ty, as in oth­er CLR-based lan­guages, it can mean dif­fer­ent things — val­ue equal­i­ty or ref­er­en­tial equal­i­ty. For val­ue types, equal­i­ty means the val­ues are iden­ti­cal. For ref­er­ence types, equal­i­ty is deter­mined by over­rid­ing the System.Object method Equals.