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
    int
but here has type
    float
> 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
    int
but here has type
    float

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:

http://download.opensuse.org/repositories/server:/http:/mongrel2/openSUSE_11.3/