F# Basic in imperative way. part 1
F# is a functional language that will be included in VS 2010 as one of development language. You can use F# in VS 2008 on CTP version that you can download from Microsoft Research as CTP Addon for VS 2008. The links is http://msdn.microsoft.com/en-us/fsharp/default.aspx
There are many differences between F# and C# / VB.NET as development language. The main differences are F# :
- Immutable by default
- Functional with type inferred
For more detailed you can see many about F# on the link above or Eriawan’s mugi blogs (if already exist) at http://mugi.or.id/blogs/eriawan/
Since F# is functional programming, you have to remember that every function MUST return something. The return can be any object or unit.
for example :
let addAngka a b = a + b
this function said that add a and b and return that value.
You can see this will return int.
let fungsi = ()
this function said that do something and return unit.
nb: unit is same as void in C#.
For detail syntax you can refer on MSDN
In college or university, most of programming practices starts with simple logic like loop or decision. F# is the same as other language that understand concept of loop. But you have to be remembered that loop in F# (especially using for …. do….) have return unit.
For example, I want to draw triangle with star (bikin segitiga dari bintang)
open System
let segitidaBintang baris =
for totalBaris = 0 to baris do
for line = 1 to totalBaris do
Console.Write("*")
Console.WriteLine()
()
If I run this method in F# interactive, it will be displayed triangle with stars based on lines that you enter.
For example :
> segitidaBintang 5;;
and will be displayed
Is it easy?
If you think so, there is a little quiz for you. Can you draw a butterfly stars in F# interactive ?
I give you samples in my interactive. The butterfly will be result in :
>
val butterflyStar : int -> unit
> butterflyStar 4;;
* *
** **
*** ***
********
*** ***
** **
* *
val it : unit = ()
> butterflyStar 5;;
* *
** **
*** ***
**** ****
**********
**** ****
*** ***
** **
* *
val it : unit = ()
>
have fun.