Ksunair’s Weblog

Just another WordPress.com weblog

Excellent F# starting point..

Posted by Unni on May 10, 2011

Anyone who is interested in learning F#, please visit the VisualFSharp.com It has excellent document on F#.

Posted in F# | Leave a Comment »

Lonestar ruby conference

Posted by Unni on September 5, 2008

F# took a back seat in last week as I was attending Lonestar Ruby conference. So far it is very good. The high light so far is a chance to meet and shake hands with one of the greatest minds Matz (for those who don’t know, he is the creator of programming language Ruby). I have been a big fan of Ruby for some time that is the reason I got in to Ironruby project as well.

I will get back to F# hopefully next week. Have a good weekend.

Posted in General | Leave a Comment »

Installing F# in Mac

Posted by Unni on August 21, 2008

So far I used on XP/Vista to do my F# expriments. At home I prefer to use Mac so I decided to install F# in Mac today. I attempted to install F# sometime back in my other Mac and I ran into lot of problem at that time. I think that was one of the reason I stayed out F# (good excuse).

Today I gave it a go and to my surprise the install was very painless except couple of caviats. Have a look at the following url for some very decent information on how to install F# on Mac. If you still have problem send me a mail..

http://cs.hubfs.net/forums/thread/4732.aspx

Here is a short version of how I went about installing.

  1. Download and install Mono. Very simple and no special instrcutions required.
  2. Download F# zip file, unzip and drop the files in a directory of your choice.
  3. At the terminal,  run the following commands “chmod +x install-mono.sh” from the root directory where this file is located. Now run “sh install.mono.sh”, this will install all the required components and you might see some errors, just ignore them.
  4. Now in the terminal itself, try “mono bin/fsi.exe” this will open up F# console. Remember, it is very slow in loading. So please be patient.
  5. One additional problem you might run into is the delete key doesn’t work. How to resolve is explained in the same link as well. But if you need help send me a mail.

Posted in F#, Introduction | Tagged: , , | 2 Comments »

The ‘let’ – continues…

Posted by Unni on August 20, 2008

As I mentioned in the previous post ‘let’ is very important for assignments and funcation definitions. Following things are the ones I learned new today and I thought I share with you all.

Lets look at the following code

  1.  
    1. let a=5;;
    2. a;;
    3. let a=6;;
    4. a;;
    5. let a = a + 1;;
    6. a;;

Atep 2 results in 5 and step 4 result in 6 and step 6 result in 7 as expected. In general we would think the variable is in stack and everytime the value is assigned then stack value is changed. That is correct in c# world but that is not true in F#.  Then how come the value did change in the previous example? Good question and here is the answer. In F# everything is immutable (of course there is exception, we will get to it later). How did the last piece of code worked. As you can imagine, when you assign the first a=5, then F# creates an immutable variable a and assings value 5. When it reaches the next assignment at step 3, it recreates new immutable variable and assignes new value and so on. Hope I am clear.

Here is one more twist that you might like. Lets try the following code

  1.  
    1. let v = 10;;
    2. v;;
    3. let v = 5 in
    4.     v + 2;;
    5. v;;
Important feature of Let

Important feature of Let

When you run the code it procedures result as shown in the above image.  It very important that when you try to see the content of variable ‘v’, you get the value assigned in the first line rather than the result of line 3 and 4. We need to understand this distinction before we delve more deep into the code. It took me some tries to understand exactly what it is.

The first line is a variable assignment and the value is preserved till the value is changed. But when you get line 3 and 4, in this case, ‘let v’ is not an assignment rather than a local funcation (it may not be a correct term). So in line 3 what happens is we are temperoraryly assigning the value 5 for the following expression to execute. What it means is the following statement v + 2 will take the value 5 and evaluate the expression and result it out as the next line in the output. So the value 7 never stored in the variable v.

Let me put it other way. When you attempt an assignment like ‘let v = 10;;’ and excute it at the prompt see what is the F# result, it says, it created a variable of type ‘int’. But when you ran step 3 and 4, it didn’t say it created a variable or funcation instead, it executed the lines and produced a result of 7 thus this value is never preserved.

it is very important that you guys understand I am beginner like you so please send me comments on if I am talking something wrong.

Posted in Introduction | Tagged: , , | Leave a Comment »

The ‘let’

Posted by Unni on August 19, 2008

Everything in F# starts with ‘let’ keyword. Variable, value or funcation you name it, it has to start with let. So lets say you want to define an integer of value 5 to a variable ‘a’ then you do it like the following

let a = 5;;

Lets say you want perform an addition of two integer variables 3 and 5 and store the result in variable say ‘b’ then you do so as following

let b = 3 + 5;;

Lets say you want to create a funcation which doubles the input value, you would do so by ‘let’ as follows;

let double x = x + x;;

In here double is the funcation name and x is the input parameter to the funcation.

Anyway, only place (in my very limited knowledge) you do not use let is to execute the command in the console mode. For example if you want to execute the double funcation for the value of 6 then at the F# console, you would do the following

let double x = x + x;;

double 6;;

The second call of double will invoke the double method defined in the previous line and pass value 6 as X and write the answer of 12 on to the console.

Couple of things to remember (for my notes)

  • F# automatically infers the type of a variable by the value assign to it.
  • If you perform an operation with mixed types, the operation will fail.
  • You can force convert the type to another type by specifying the anticipated type infront before the call.

Posted in Introduction | Tagged: , , | Leave a Comment »

F# Baby Step – 1

Posted by Unni on August 15, 2008

First think you need to do is to install F# in your computer (OS X or Windows). I am not going to explain how to install F# because the instructions at F# web site is very good source.

Once you installed you would see under Start-> Programs -> Microsoft Research F#

Something like the following. For our initial learning process I am going to stick to the first option F# Interactive (Console).

Let us write our first code. Normally people write “Hello World”, but I am not, I am going add two integer numbers. With this we can learn how to assign values to variables and how to perform an addition operation. Following picture shows the code and the result.

Adding 2 numbers in F#

Adding 2 numbers in F#

Each statement is seperated by ;; and each assignment uses ‘let’ keyword followed by variable name and then the value. One interesting part about the assignment statement is that the type. You didn’t declare the type of the variable but it was inferred from the value on the right hand side. In the first assignment the value is 3 and it is inferred as integer. That is what shown right below the let statement.

The line following let a = 5;; is val a : int, it means value a is of type integer. The same applies to the next let statement. The arithmatic statement

let c= a + b;;

also inferred the type based on the value of a and b and in this case value of c is integer. Let statement only created the assignment of the arithmatic operation but it didn’t really excute the statement. So to get the result you need to issue the last statement that is

c;;

this basically returns the value of c. I still need to learn some more basics of this assignment and operations.

Just for fun try

>3 + 4.0;;

it will fail. F# doesn’t do any automatic boxing and unboxing. So in this case, we are trying to add int with float and F# will throw an error. If you want to fix this, you need explicitly make this type same type like following

>3 + int 4.0;;

Posted in Introduction | Tagged: , , | Leave a Comment »

Starting F#

Posted by Unni on August 13, 2008

For last couple of months I have been trying to get a handle of F# whenever I got a chance. I was reading books and trying out samples and finally I am getting a hang of it. Even though I know it is going to be long hard road for me, I am going to give a full try. When I started reading F#, I was doing it for fun to see what it has to offer and in the begining I concluded that it is more of an academic than commercial use. But more and more I read I see its place in common work place if used wisely. I can not explain them right now since I can not put them down properly yet since I am still exprimenting with that.

There are lof nice blogs, book and groups available to get started quickly. If you know any kind of funcational programming then it would be right at home for you. But for me, I started my development in procedural programming. When OOP model came out, I was still coding in COBOL and then I was forced to work on OOP model without much of any training or reading. It was a mess in the begining but finally I was able to understand the concept and able to appriciate it. One of the reason I started reading F# is to have a head start before it too late like last time (more selfish).

This blog series more of my notes as I learn F#.

Posted in Introduction | Tagged: , | Leave a Comment »