Getting Started with F# (Part I)

Posted: April 4, 2011 in F#
Tags:

Hi, now i’m learning new programming language from .NET Framework, it’s F#. This new language is different from other .NET languages such as C# or VB. F# is designed to be simple yet powerful.

If we learn a new programming language, first step to do is how to print “hello world” :) and how to create it is quite simple.

Example below is code of hello world program. open System is similar to include in C#, open System is essential if we’re going to use function like Console.WriteLine or Console.ReadKey. To ending an expression, F# uses whitespace instead semicolon(;). To describe a function in F# we use keyword let and character “_’ is the name of main function in F#.

To print “hello world” message in console we use Console.WriteLine “hello world” and for preventing console window closing automatically we use Console.ReadKey().

open System

let _ =
 Console.WriteLine "hello world"
 Console.ReadKey()

This is an example for creating function and simple addition. There’s function called add. In add function we have two parameters x and y. Function in F# don’t declare its parameters or return value  explicitly.

open System

let add x y =
    x + y

let _ =
    let a = add 1 2
    Console.WriteLine a
    Console.ReadKey()

add function is similar to C# code below

int add (int x, int y)
{
    return x + y;
}

Now i’ll explain about currying. Look at code below:

let _ = 
    let x = add 1
    let a = x 2
    Console.WriteLine a
    Console.ReadKey()

We call add function with just one parameter and store the result in x variable. Actually x is not value but another new function which can be called with an additional parameter.

Leave a comment