• f# essential - part 9 - no need to use ref record type in class

    As I discussed it in post f# essential - part 5 - mutable, ref, ref record is used for closure. And mutable is similar concept of variable in c#. So that you can not use mutable defined outside of a function. So the following it is wrong

    let build_t() = 
        let mutable x = 0
        let t() =
            x <- 1 + x
            printfn "you call me %i time(s)" x
        t
    
    
  • f# essential - part 8 - talking to c# library

    When I found that syntax in f# to support curry, I am very excited. May be we can do the same thing with .net library written in c#. For example, maybe I can use Console.WriteLine as a function to write the following code.

    let greet = Console.WriteLine "hello, {0}"
    greet "fred"
    Console.WriteLine "hello, {0}" "fred"
    

    The compiler give me an error "The value is not a function and can not be applied". So F# function is different from c# method. To call this method, you have to use a tuple syntax. But you can wrap a method easily into a f# function, then you can do curry

    Console.WriteLine ("hello, {0}", "fred")
    
    
  • f# essential - part 7 Curry

    Function application, also sometimes referred to as function composition or composing functions is a big thing in functional programming. One style of composition is called Curry. Curry is feature of lots functional language. When you have a function with two parameter, you can pass one argument to the function, that create an new partial function, which accept one parameter. This partial function is also called curried function. F# support this in a very elegant syntax.

    let print x y = 
         printfn "%i" x 
         printfn "%s" y 
    let print100y = print 100 //create a curried function 
    print100y "hello" //call the curried function 
    

    If you want to fill in the second argument to create a new function, this is also support, like below.

    let printx_hello x = dosomething x "hello" 
    printx_hello 100
    
  • 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