• f# essential - part 12 - function is not method, is not delegate, what is it anyway

    As previous post discussed, f# function is not a method, because it support closure, it curry and so on, and .net method does not support these. It looks very much like a delegate (aka lambda, anonymous method) in .net. For example, the following looks like it is a delegate.

    open System
    open System.Linq
    
    
  • f# essential - part 11 - ignore function

    If you call a function which does not return unit type, you can not call it like the following.

    let f1() = 
        //do some side effect stuff
        "f1"
    f1()
    

    You have to do the following

    f1() |> ignore
    //or 
    let _ = f1()
    
  • f# essential - part 10 - why and when f# does not need null value,

    I am surprise when I don't see a need to use null in f#. It use a generic record type "option", which is a very elegant solution to express nullness. In c#, we have reference type and value type, in .net 1/1.1, we express nullness for reference type using "null", in .net 2 above, we can use Nullable type of value type. However, f# unify this using option.

    The null keyword is a valid keyword in the F# language, and you have to use it when you are working with .NET Framework APIs or other APIs that are written in another .NET language. The two situations in which you might need a null value are when you call a .NET API and pass a null value as an argument, and when you interpret the return value or an output parameter from a .NET method call. For more please refer to MSDN

  • 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")