F# Hello World

Ok, so lets take a look at a really simple F# program. It’s just one line of actual code.

let helloworld ruready = if ruready = 'y' then "hello functional world" else "stickin with c#";;

The above line declares a function. The left-hand-side of the expression begins with the let keyword. The let keyword is the most important keyword in F# and it is used to bind names to values. Here we are binding helloworld to the expression on the right hand side. In addition to that, the word ruready next to the function name is an argument to the function. With that in mind, our definition of the syntax to declare a function might look something like this:

let methodName x1 ... xn = expression

Now lets look at the code on the right-hand-side of the function declartion. The if-then-else conditional control structure should look pretty familiar to us. Here we're testing our ruready argument to determine if it equals the char 'y'. If it does, the function evaluates to "hello functional world", else it evaluates to "stickin with c#". The ";;" tells the Interactive to begin executing your code.

Lets take that hunk of code and paste it into the F# Interactive and hit <enter>. The Interactive responds with the following line:

val helloworld : char -> string

This may seem quite confusing at first but it's not. The first word val is simply short for value. At this point you can consider the term "value" analogous to "variable" in other languages. On the right-hand-side you see "char -> string". This tells you that the function helloworld takes one argument which is of type char and returns a string.

You'll notice that when you defined your function you didn't specify any type information. How did the F# Interactive know that your function takes a char as an argument and returns a string? It did this using one of the fundamental characteristics of F#, type inference. We'll discuss type inference more as we continue the tutorial.

Now let's try and invoke this function. Copy and paste the following into the F# Interactive:

>printfn "%s" (helloworld 'y');;

Here we're using the printfn function to write our string to the screen. The printfn function writes to the standard output stream using the .Net System.Console.Out function. The %s in the string argument to the printfn is a placeholder for the string being returned by our helloworld function. You should get the following output:

hello functional world
val it : unit = ()

As you can see the string we expected, "hello functional world", was returned from our function and the printfn function wrote it out to the screen. The line below our outputted string, " val it : unit = ()" essentially describes the printfn function as having returned nothing. The unit type in F# is similar to the void type in C# and other languages. The empty parenthesis () are simply the literal value that represents the unit type.

So there we are, an F# hello world program. Pretty easy huh? Ironically it was pretty imperative. Oh well, we'll begin doing some hot functional programming soon!

Comments
Tim Merrifield Sr.  1/26/2008

Good work!
  8/20/2008

hellllllo
Leave Us a Comment!
Title (we need it)

Your Name (we need it)

Your Comment (we need it)