Dis­claimer: I do not claim credit for the code exam­ples and much of the con­tents here, these are mostly 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 included a lot of code exam­ples for you to try out yourself.

Prim­i­tive Types

F# is sta­t­i­cally 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 numeric types (both inte­ger and floating-point) with their suffixes:

image

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

clip_image001

There are no implicit type con­ver­sion in F#, which elim­i­nates sub­tle bugs intro­duced by implicit type con­ver­sion as can be found in other languages.

Arith­metic Operators

You can use stan­dard arith­metic oper­a­tors on numeric prim­i­tives, like other 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­ported operators:

image

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 versa:

clip_image002

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

image

Big­Int

If you are deal­ing with data larger than 2^64, F# has the Big­Int type for rep­re­sent­ing arbi­trar­ily 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­ily large integers.

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

clip_image003

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

Bit­wise Operations

Prim­i­tive inte­ger types sup­port bit­wise oper­a­tors for manip­u­lat­ing val­ues at a binary level:

image

Char­ac­ters

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

clip_image004

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

image

You can get the byte value of a char­ac­ter lit­eral by adding a B suffix:

clip_image005

Strings

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 within a string, use the indexer syn­tax, .[ ], and pass in a zero-based char­ac­ter index. For example:

clip_image006

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

clip_image007

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

clip_image008

Boolean Val­ues

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

image

F# uses short-circuit 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 value won’t be eval­u­ated. For example:

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.

Com­par­i­son and Equality

You can com­pare numeric val­ues using stan­dard oper­a­tors listed below:

image

All these oper­a­tors eval­u­ate to a Boolean value 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 second.

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

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

Share

Leave a Reply