With VS2010, for creating, debugging and running F# projects, you do not need to jump hoops. I was reading F# for Scientists for some time and is an excellent book, this is coming from a person who is neither a scientist nor functional programmer.
Here is our kick start into the world of F#.
Start up your VS2010 and choose F# in the languages
I chose F# application and name your project. Type in the following code (under Program.fs)
let message = "Hello World" let i = 5 let sum x = i + x let anonSum = fun x y -> x + y let main() = printfn "%A" message printfn "%A" i printfn "%A" (sum 6) printfn "%A" (anonSum 6 6) main()
that is it, now run the program will open up a console output and show the result and close the window automatically. So I hope you checked your result fast enough before windows closing the screen.
We can always add a line in the code for the program to wait for ‘press ANYKEY’ to continue’. I still remember the ‘ANYKEY’ joke, where the use of Window call in support since the user is unable to find ANYKEY in the keyboard. Anyway, before we go into adding key board listener before closing the console window, lets try it differently.
In F# you have an option to try all the code or part of it interactively using F# Interactive, it like trying Linq using LinqPad. To do that, all you have to do is to select the code you want to try in interactive screen and press Alt + Enter or right click and select ‘Send to Interactive’ option. Which will open ‘F# Interactive’ window and run the code. So if you would have selected all the lines from the above code and did send to interactive your result would be something like the following
Lets say you are not a interactive person and want to see the result by running the code, then all you have to do is add a line to wait for user to press any key. Which is accomplished by adding System name space and use ‘Console.Readkey’ like C#. Remember F# is first class citizen in .Net so all name spaces available in other languages available to F# as well. So here is the modified code
open System let message = "Hello World" let i = 5 let sum x = i + x let anonSum = fun x y -> x + y let main() = printfn "%A" message printfn "%A" i printfn "%A" (sum 6) printfn "%A" (anonSum 6 6) Console.ReadKey(true) main()
As you can see, now we are using System name space by calling ‘open System’. Also in the main function the last call is to read the keyboard but do not display the result back to the screen. Now if you would run it, you get the following output and the console is patiently waiting for you to press any key.
Here is the catch, if you would use the code and try run it interactively, you will see ‘application does not have a console’ error but the code does run and generate the result. To solve that problem and also that you want to run the program in console mode, change the Console.Readkey to Console.Read, that is it, now if you would the program, it will run same way as before, it waits for key stroke and also if you would select all the code and run it in F# interactive, it will run without throwing any errors. By the way, I did not magically found the solution, the error message in the F# interactive gave me the suggestion to change Console.Readkey to Console.Read.
There are few things I did not go over like what is main, why there is a blue wavy under main, why Readkey to Read worked etc., that is for later discussion.