0% found this document useful (0 votes)
5 views8 pages

F# Keywords and Basic Program Guide

The document provides an overview of F# keywords, their usage, and data types. It explains reserved keywords, comments, and includes a basic program example that prints 'Hello World'. Additionally, it details various integral data types in F#, including their sizes and ranges.

Uploaded by

gobobi8535
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views8 pages

F# Keywords and Basic Program Guide

The document provides an overview of F# keywords, their usage, and data types. It explains reserved keywords, comments, and includes a basic program example that prints 'Hello World'. Additionally, it details various integral data types in F#, including their sizes and ranges.

Uploaded by

gobobi8535
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

F#

Used in asynchronous workflows to bind a name to the result of an


let! asynchronous computation, or, in other computation expressions,
used to bind a name to a result, which is of the computation type.

match Used to branch by comparing a value to a pattern.

member Used to declare a property or method in an object type.

Used to associate a name with a group of related types, values, and


module
functions, to logically separate it from other code.

mutable Used to declare a variable, that is, a value that can be changed.

Used to associate a name with a group of related types and modules,


namespace
to logically separate it from other code.

Used to declare, define, or invoke a constructor that creates or that


can create an object.
new
Also used in generic parameter constraints to indicate that a type
must have a certain constructor.

Not actually a keyword. However, not struct in combination is used


not
as a generic parameter constraint.

Indicates the absence of an object.


null
Also used in generic parameter constraints.

Used in discriminated unions to indicate the type of categories of


of
values, and in delegate and exception declarations.

Used to make the contents of a namespace or module available


open
without qualification.

16
F#

Used with Boolean conditions as a Boolean or operator. Equivalent


or to ||.

Also used in member constraints.

Used to implement a version of an abstract or virtual method that


override
differs from the base version.

private Restricts access to a member to code in the same type or module.

public Allows access to a member from outside the type.

rec Used to indicate that a function is recursive.

Used to indicate a value to provide as the result of a computation


return
expression.

Used to indicate a computation expression that, when evaluated,


return!
provides the result of the containing computation expression.

Used in query expressions to specify what fields or columns to


extract. Note that this is a contextual keyword, which means that it
select
is not actually a reserved word and it only acts like a keyword in
appropriate context.

Used to indicate a method or property that can be called without an


static instance of a type, or a value member that is shared among all
instances of a type.

Used to declare a structure type.

struct Also used in generic parameter constraints.

Used for OCaml compatibility in module definitions.

then Used in conditional expressions.

17
F#

Also used to perform side effects after object construction.

to Used in for loops to indicate a range.

true Used as a Boolean literal.

Used to introduce a block of code that might generate an exception.


try
Used together with with or finally.

Used to declare a class, record, structure, discriminated union,


type
enumeration type, unit of measure, or type abbreviation.

upcast Used to convert to a type that is higher in the inheritance chain.

Used instead of let for values that require Dispose to be called to


use
free resources.

Used instead of let! in asynchronous workflows and other


use! computation expressions for values that require Dispose to be called
to free resources.

Used in a signature to indicate a value, or in a type to declare a


val
member, in limited situations.

Indicates the .NET void type. Used when interoperating with other
void
.NET languages.

Used for Boolean conditions (when guards) on pattern matches and


when
to introduce a constraint clause for a generic type parameter.

while Introduces a looping construct.

with Used together with the match keyword in pattern matching


expressions. Also used in object expressions, record copying

18
F#

expressions, and type extensions to introduce member definitions,


and to introduce exception handlers.

yield Used in a sequence expression to produce a value for a sequence.

Used in a computation expression to append the result of a given


yield! computation expression to a collection of results for the containing
computation expression.

Some reserved keywords came from the OCaml language:

asr land lor lsl lsr lxor mod sig

Some other reserved keywords are kept for future expansion of F#.

atomic break checked component const constraint constructor

continue eager event external fixed functor include

method mixin object parallel process protected pure

sealed tailcall trait virtual volatile

Comments in F#
F# provides two types of comments:

 One line comment starts with // symbol.

 Multi line comment starts with (* and ends with *).

19
F#

A Basic Program and Application Entry Point in F#


Generally, you don’t have any explicit entry point for F# programs. When you compile an F#
application, the last file provided to the compiler becomes the entry point and all top level
statements in that file are executed from top to bottom.

A well-written program should have a single top-level statement that would call the main loop
of the program.

A very minimalistic F# program that would display ‘Hello World’ on the screen:

(* This is a comment *)
(* Sample Hello World program using F# *)
printfn "Hello World!"

When you compile and execute the program, it yields the following output:

Hello World!

20
5. F# – Data Types F#

The data types in F# can be classified as follows:

 Integral types

 Floating point types

 Text types

 Other types

Integral Data Types


The following table provides the integral data types of F#. These are basically integer data
types.

F# Type Size Range Example Remarks

42y 8-bit
sbyte 1 byte -128 to 127 signed
-11y integer

42uy 8-bit
byte 1 byte 0 to 255 unsigned
200uy integer

42s 16-bit
2
int16 -32768 to 32767 signed
bytes -11s integer

42us 16-bit
2
uint16 0 to 65,535 unsigned
bytes 200us integer

int / int3 42 32-bit


4
-2,147,483,648 to 2,147,483,647 signed
2 bytes -11 integer

21
F#

42u 32-bit
4
uint32 0 to 4,294,967,295 unsigned
bytes 200u integer

42L 64-bit
8 -9,223,372,036,854,775,808 to
int64 signed
bytes 9,223,372,036,854,775,807 -11L integer

42UL 64-bit
8
uint64 0 to 18,446,744,073,709,551,615 unsigned
bytes 200UL integer

42I
1499999
At 9999999 arbitrary
bigint least 4 any integer precision
bytes 9999999 integer
9999999
9999I

Example

(* single byte integer *)


let x = 268.97f
let y = 312.58f
let z = x + y

printfn "x: %f” x


printfn "y: %f” y
printfn "z: %f” z

(* unsigned 8-bit natural number *)

let p = 2uy
let q = 4uy
let r = p + q

22
F#

printfn "p: %i" p


printfn "q: %i" q
printfn "r: %i" r

(* signed 16-bit integer *)


let a = 12s
let b = 24s
let c = a + b

printfn "a: %i" a


printfn "b: %i" b
printfn "c: %i" c

(* signed 32-bit integer *)

let d = 212l
let e = 504l
let f = d + e

printfn "d: %i" d


printfn "e: %i" e
printfn "f: %i" f

When you compile and execute the program, it yields the following output:

x: 1
y: 2
z: 3
p: 2
q: 4
r: 6
a: 12
b: 24

23

You might also like