Difference between revisions of "FSharp"

From ScienceZero
Jump to: navigation, search
(Functions)
(Values and Variables)
Line 7: Line 7:
 
  '''let mutable b = 2'''
 
  '''let mutable b = 2'''
 
  This is a variable and it can be updated with a new value like in other laguanges
 
  This is a variable and it can be updated with a new value like in other laguanges
  like this '''b <- b + 1'''
+
  '''b <- b + 1'''
 +
 
 +
 
 +
== Types ==
 +
The compiler infers types in most cases.
 +
'''let a = 7'''        - 32 bit integer
 +
'''let b = "Hello"'''  - String
 +
'''let c = 3.1415'''  - 64 bit floating-point
 +
 
 +
In some cases it is not possible to infer the type or you want to make a point out of it.
 +
'''let a = (xPos : uint64)'''
 +
 
 +
'''Type  Suffix  .NET Type      Range'''
 +
byte      uy  System.Byte    0 to 255
 +
sbyte      y  System.SByte    −128 to 127
 +
int16      s  System.Int16    −32 768 to 32 767
 +
uint16    us  System.UInt16    0 to 65 535
 +
int, int32    System.Int32    −2<sup>31</sup> to 2<sup>31</sup> − 1
 +
uint32      u  System.UInt32  0 to 2<sup>32</sup> − 1
 +
int64      L  System.Int64    −2<sup>63</sup> to 2<sup>63</sup> − 1
 +
uint64    UL  System.UInt64  0 to 2<sup>64</sup> − 1
 +
float          System.Double  64 bit floating-point IEEE 64, approximately 15 significant digits.
 +
float32    f  System.Single  32 bit floating-point IEEE 32, approximately 15 significant digits.
 +
decimal    M  System.Decimal  A fixed-precision floating-point type with precisely 28 digits of precision.
  
 
==Functions==
 
==Functions==

Revision as of 07:02, 23 August 2013

Values and Variables

let a = 2
This is a value and it is constant within the scope of the value.
You can safely pass values to other threads, computers or to the other side of the world.
Always use values instead of variables when possible.
let mutable b = 2
This is a variable and it can be updated with a new value like in other laguanges
b <- b + 1


Types

The compiler infers types in most cases.
let a = 7        - 32 bit integer
let b = "Hello"  - String
let c = 3.1415   - 64 bit floating-point
In some cases it is not possible to infer the type or you want to make a point out of it.
let a = (xPos : uint64)
Type   Suffix  .NET Type       Range
byte       uy  System.Byte     0 to 255 
sbyte       y  System.SByte    −128 to 127 
int16       s  System.Int16    −32 768 to 32 767 
uint16     us  System.UInt16    0 to 65 535 
int, int32     System.Int32    −231 to 231 − 1 
uint32      u  System.UInt32   0 to 232 − 1 
int64       L  System.Int64    −263 to 263 − 1
uint64     UL  System.UInt64   0 to 264 − 1 
float          System.Double   64 bit floating-point IEEE 64, approximately 15 significant digits.
float32     f  System.Single   32 bit floating-point IEEE 32, approximately 15 significant digits.
decimal     M  System.Decimal  A fixed-precision floating-point type with precisely 28 digits of precision.

Functions

Everything is a function and returns a value. Functions have only one exit.

let max a b = if a > b then a else b
This is a function called max that takes two integers as input and returns the larger of them.
It uses IF as a function.
If it makes no sense for a function to return a value it should return a value of type unit
like this let a = ()
If the value returned from a function is not required it can be deleted by using the ignore function.
like this ignore (max 1 3)

Loops

Try to avoid loops when it is simple to do so because most loops require change of state to terminate and change of state is an opportunity for bugs.

while(true) do
    printfn "Hello World"
for i = 1 to 10 do
    printfn "%A" i
// This type of loop is quite safe
let arr = ["One";"Two";"Three"]
for t in arr do
    printfn "%A" t