-
f# essential - part 6 - tuple
In other language, a method can not return more than one parameter. There are two way to get around this, use passed-by-reference parameter, or use composite type to encapsulate two or more fields in to one object. Both of them are not elegant. However, f# can express this using a very simple syntax to solve this problem, "tuple", which is a strongly typed structure. Here is an example.
let (x, y, z) = (1, 2, 3) //or let x, y, z = 1, 2, 3
-
f# essential - part 5 - mutable, ref
As mentioned in previous post, the "let" syntax define a identifier binding, which is immutable. So it is not variable in the sense of traditional programming. But we can not programming in pure functional language environment. We need to create state, and side effect. F# is pragmatic and play nice with .net framework, so it has to be support variable. To use variable, you just need to add a "mutable" in front identifier, then the identifier became variable in the sense of traditional programming.
let totalArray2 () = let array = [|1; 2; 3|] let mutable total = 0 for x in array do total <- total + x
-
f# essential part 4 - closure
f# support closure. Here is the definition of closure
In computer science, a closure (also lexical closure, function closure or function value) is a function together with a referencing environment for the nonlocal names (free variables) of that function.
In f#, you can define functions within other functions. The inner function can use any identifier in scope, including identifiers defined in outer function. When the inner identifier return as result of the outer function, identifiers defined in outer function is still being captured by the inner function, the inner is called closure.
let buildGreet greeting = let prefix = Printf.sprintf "%s ," greeting let greet name = Printf.sprintf "%s%s" prefix name greet //greet is a closure, even the it is return it can still access,
-
f# essential - part 3 matching shortcut
F# provide a shortcut to define a function which purely use input parameter for matching purpose. For example the following are equal.
let printInt = function 1 -> printfn "1" | 2 -> printfn "2" | _ -> printfn "something else"
-
f# essential - part 2 - pipleline operator
Before I discuss pipeline operator, let's talk about operator. F# allow us to define operator as function. The following are infix operators
$ % & * + - . / < = > @ ^ |
The following are prefix operators.
! ? ~
You can define a infix operator like below
let (operator) ops1 ops2 = ... //for example let (+) a b = a - b 2+3
You can define a prefix operator like below
let (!) a = 1 !8
So you can change your expression from "add 2 4" to "2 + 3" or "m1 a" to "!a", let's defined our pipleine operator as below.
let (-->) x f = f x let rb = 0.5 --> System.Math.Cos
However, F# already define the "|>" for us, so we should use "|>". Operator is not a function, definition, rather you should think of it as macro. The compiler will change the syntax before it compile.