F# – Working classes

Lately I am trying improve my programming by following some Katas. One of the things, I have been trying to learn was Functional Programming. So I decided to spend more time and try to write the same code I write in C# to see how can I adopt and learn faster. So in this blog, we will look at a small program I was trying to do in C#

1. Using third party control:

To use third party control, you do exactly same as you would do in C#, add the reference and then instead of using ‘using namespace’ in F# you would open the namespace to use in the code. In this example I am using bcParser.Net.dll to evaluate arithmetic expression. So the open would look like something like the following

open Bestcode.Mathparser

2. Working with a class:

Now that we added reference to the namespace we need to instantiate the class and add arithmetic expression to it to use it. For our example, our expression is “1+2” and expect the result to 3. To instantiate the class

let mathParser = MathParser()

now that mathParser is the instance of the class, we need assign the expression to mathParser expression member variable. This is done little different than C#. In C# we will do some thing like the following

mathParser.Expression = “1+2”

but in F#, you have to do the following

mathParser.Expression <- “1+2”

3. Printing the result:

Now that we have the expression set, we need to evaluate and print the result to console. This part is very simple

printfn “%s” (mathParser.Value.ToString())

make sure you put the whole thing parenthesis otherwise you will get compiler error.

One thing I noticed while using F#, the compiler errors are so explanatory, you will able to figure out what were you doing wrong in the code.

Lets go back F#

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

image

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

image

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.

image

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.

Digg This