Tuesday, November 30, 2010

Studying F# : catch the exception

In ruby, the clauses for catching some exception is like below:
  # some actions
  # the action when error occurs
  # the action when error does not occur
  # the final action whether error occurs or not

But, in F#, there are two point that is different from ruby.

  1. There are no clause corresponding else.
  2. There are only clauses either try/with(corresponding begin/rescue) or try/finally(corresponding begin/ensure)

In other words, clause try/with/finally is Forbidden.

> let results =    
-     try
-         (1 / 0)
-     with
-         | ex -> "Some error occurs."
-     finally
-         "Final";;


/Users/youhei/stdin(9,5): error FS0010: Unexpected keyword 'finally' in binding. Expected incomplete structured construct at or before this point or other token.

Saturday, November 27, 2010

Both openSUSE and Mono will be alive, maybe.

4 days ago, Novell agreed to be bought by Attachmate. And my interest is "Will both openSUSE and Mono be alive, or not?".

For Mono, there is a Miguel de Icaza's tweet.
After the Novell acquisition, Mono continues as-is, but our paychecks will come from Attachmate instead of Novell.

For openSUSE, there is a n article that says "openSUSE is safe".

I hope both projects are alive.

Wednesday, November 24, 2010

One more instruction for installing MonoDevelop F# add-in on Linux

If you install MonoDevelop F# add-in on Linux and you use with non-root user, you should do one more instruction:
sudo chmod a+w -R /etc/mono/registry/

Monday, November 22, 2010

F# in MonoDevelop on Mac OS X

In the article "F# in MonoDevelop and cross-platform web sites & screencasts", there are the instruction for installing F# in MonoDevelop on Linux. But someone ( of course, me too ) want to use this on Mac OS X and that instruction not suitable for on Mac OS X. So I introduce in this post.

0. Install F#

see also in this post.

Friday, November 19, 2010

.NET Book Zero equals Mono Book Zero

.NET Book Zero is the most famous book as free and online PDF, for C# beginners.

But, of course, this book also useful for Mono beginners like me. I am reading both this book and Professional F# 2.0.

Tuesday, November 16, 2010

Start reading Professional F# 2.0

I have started reading "Professional F# 2.0".
No I'm reading the first chapter: PART-0-CHAPTER-1. This introduce where we have to use functional programming in, with showing us many complicated C# codes. Yes, at now, F# codes have not appeared yet.

Saturday, November 13, 2010

Euler's formula in F#

  1. Create new F# application on VS2010
  2. Check the references containing System.Numerics
Then edit the code like below:
open System
open System.Numerics

let euler_formula = Numerics.Complex.Exp(Numerics.Complex(0.0, -Math.PI))
printfn "%O" euler_formula

At last, launch the command prompt with F5:
(-1, -1.22460635382238E-16)
It occurs by inexactness of floating-point.

Wednesday, November 10, 2010

Do you know Ruby 1.9 encoding rule?

Ruby 1.9 treats the encoding within each String instance. And there are rules which encoding are defined from script or from external file. The rules are there (in Japanese).

I think, CRuby is the one and only Ruby implementation treating this rules correctly. When I had used irb, I had found its bug in JRuby and in IronRuby, and have reported this for each projects. In JRuby, this bug will be fixed for v1.6 release.

Monday, November 8, 2010

Studying F# : Dictionary (a.k.a. Hash or Map)

In F#, there are no types like Dictionary, Hash, Map and so on. We must implement using List, Tuple and dict function.
(This above sentence is incorrect. I correct by thie entry.)

In F#, there are no ways to create Dictionary directly. We must create Dictionary using List, Tuple and dict function.

> let dictionary_1 = dict [ (1, "one"); (2, "two"); (3, "three"); (4, "four") ];;
val dictionary_1 : System.Collections.Generic.IDictionary

> let dictionary_2 = dict [ (1, "one"); (2.0, "two"); (3, "three"); (4, "four")];;
  let dictionary_2 = dict [ (1, "one"); (2.0, "two"); (3, "three"); (4, "four")];;

stdin(7,40): error FS0001: This expression was expected to have type
but here has type
> let dictionary_3 = dict [ ("one", 1); ("two", 2); ("three", 3.0); ("four", 4)];;
  let dictionary_3 = dict [ ("one", 1); ("two", 2); ("three", 3.0); ("four", 4)];;

stdin(8,61): error FS0001: This expression was expected to have type
but here has type

Each key and value must be same types.

Friday, November 5, 2010

Studying F# : String literal

In Ruby, there are two types for String literal. The first one is the literal not-escaping at all:
irb(main):001:0> s1 = "Hello World\n"
=> "Hello World\n"
irb(main):002:0> print s1
Hello World
=> nil
And the second one is escaping all:
irb(main):003:0> s2 = 'Hello World\n'
=> "Hello World\\n"
irb(main):004:0> print s2
Hello World\n=> nil

In F#, String literal is only using double-quote and this way is not escape at all.
> let s1 = "Hello World\n";;

val s1 : string = "Hello World

> printf "%s" s1;;
Hello World
val it : unit = ()

How do we escape all? The answer is using @ before double-quote, like below:
> let s2 = @"Hello World\n";;

val s2 : string = "Hello World\n"

> printf "%s" s2;;
Hello World\nval it : unit = ()

Tuesday, November 2, 2010

Mongrel2 repositry for openSUSE 11.3

As you know, "mongrel" is Ruby's web server as substitute of Webrick. But, "mongrel2" is not only for Ruby. "mongrel2" is for all of modern languages. And, its repositry for openSUSE 11.3 is alreday in there: