Difference between revisions of "FSharp"
From ScienceZero
(Created page with "==Loops== Try to avoid loops when it is simple to do so because loops require change of tate to terminate and change of state is an opportunity for bugs. while(true) do pr...") |
(u) |
||
Line 1: | Line 1: | ||
+ | == Values and Variables == | ||
+ | This is a value and it is constant within the scope of the value. | ||
+ | let a = 2 | ||
+ | |||
+ | This is a variable and it can be updated witha new value. | ||
+ | let mutable b = 2 | ||
+ | b <- b + 1 | ||
+ | |||
+ | |||
+ | ==Functions== | ||
+ | Everything is a function and returns a value. | ||
+ | let max = if a > b then a else b | ||
+ | |||
+ | If it makes no sense for a function to return a value it should return a value of type unit. | ||
+ | let a = () | ||
+ | |||
+ | If the value returned from a function is not required it can be deleted by using the ignore function. | ||
+ | ignore (1 + 2) | ||
+ | |||
+ | |||
==Loops== | ==Loops== | ||
− | Try to avoid loops when it is simple to do so because loops require change of | + | 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 | while(true) do | ||
Line 8: | Line 28: | ||
printfn "%A" i | printfn "%A" i | ||
+ | // This type of loop is quite safe | ||
let arr = ["One";"Two";"Three"] | let arr = ["One";"Two";"Three"] | ||
for t in arr do | for t in arr do |
Revision as of 06:28, 23 August 2013
Values and Variables
This is a value and it is constant within the scope of the value.
let a = 2
This is a variable and it can be updated witha new value.
let mutable b = 2 b <- b + 1
Functions
Everything is a function and returns a value.
let max = if a > b then a else b
If it makes no sense for a function to return a value it should return a value of type unit.
let a = ()
If the value returned from a function is not required it can be deleted by using the ignore function.
ignore (1 + 2)
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