Saturday, March 30, 2013

F# Slice

(In learning from "Programming F# 3.0, 2nd Edition")

Ruby's indexer can take range as parameter. In F#, Item property cannot do that. But, in F", GetSlice property cannot do that and it is called "Slice".

defining
member this.GetSlice(lowerBound : int option, upperBound : int option) =
  ...
accessing
xxx.[1..42]
xxx.[1..]
xxx.[..42]

Both two arguments are option type.

Thursday, March 28, 2013

F# Indexer

(In learning from "Programming F# 3.0, 2nd Edition")

Like ruby, F# can define indexer as defining Item property.

defining
member this.Item (idx : int) =
  ...
accessing
xxx.[42]


And also like ruby, indexer can take one more arguments.

defining
member this.Item (prefix : string , idx : int) =
  ...
accessing
xxx.["Answer", 42]

Monday, March 25, 2013

F# 3.0 is also available on FreeBSD Ports Collection

Finally, F# 3.0 is available on FreeBSD Ports Collection. Mono 3.0 port has been fixed its bug related about SGen (PR176030) and F# port has been upgraded to 3.0 (PR176018).

To install:
# cd /usr/ports/lang/fsharp
# make install clean

To check mono and fsharp with sgen:
$ mono --version
Mono JIT compiler version 3.0.3 (tarball Sat Mar 23 16:58:43 JST 2013)
Copyright (C) 2002-2012 Novell, Inc, Xamarin Inc and Contributors. www.mono-project.com
        TLS:           __thread
        SIGSEGV:       altstack
        Notification:  kqueue
        Architecture:  amd64
        Disabled:      none
        Misc:          softdebug 
        LLVM:          supported, not enabled.
        GC:            Included Boehm (with typed GC and Parallel Mark)
$ mono --gc=sgen --version
Mono JIT compiler version 3.0.3 (tarball Sat Mar 23 16:58:43 JST 2013)
Copyright (C) 2002-2012 Novell, Inc, Xamarin Inc and Contributors. www.mono-project.com
        TLS:           __thread
        SIGSEGV:       altstack
        Notification:  kqueue
        Architecture:  amd64
        Disabled:      none
        Misc:          softdebug 
        LLVM:          supported, not enabled.
        GC:            sgen
$ view `which fsharpc`
...
MONO_GC_OPTIONS=--gc=sgen
if test x$1 = x--gc=boehm; then
  shift
  MONO_GC_OPTIONS=--gc=boehm
fi
...
$ fsharpi

F# Interactive for F# 3.0 (Open Source Edition)
Freely distributed under the Apache 2.0 Open Source License

For help type #help;;

>

Friday, March 22, 2013

Tail recursive fibonacci

Tail recursive fibonacci is similar to a fibonacci using Seq.unfold
let fibonacci n =
  let rec tailRecursiveFibonacci n accum'next accum =
    if n <= 0 then
      accum
    else
      tailRecursiveFibonacci (n - 1) (accum + accum'next) accum'next
  tailRecursiveFibonacci n 1L 0L

> fibonacci 0;;
val it : int64 = 0L
> fibonacci 1;;
val it : int64 = 1L
> fibonacci 2;;
val it : int64 = 1L
> fibonacci 3;;
val it : int64 = 2L
> fibonacci 4;;
val it : int64 = 3L
> fibonacci 5;;
val it : int64 = 5L
> fibonacci 6;;
val it : int64 = 8L

If you want to fibonacci sequence itself, you should use Seq.unfold way. But if you want Fib(n) value directly, you should this tail recursive fibonacci.

Tuesday, March 19, 2013

Checked module

(In learning from "Programming F# 3.0, 2nd Edition")

Operators.Checked Module (F#) (in Japanese)

F#'s Microsoft.FSharp.Core.Operators.Checked is to check which signed numbers are overflowed or not.
> let maxInt = System.Int32.MaxValue;;

val maxInt : int = 2147483647

> maxInt + 1;;
val it : int = -2147483648
> maxInt - 1;;
val it : int = 2147483646
> open Checked;;
> maxInt + 1;;
System.OverflowException: 算術演算の結果オーバーフローが発生しました。
   場所 <StartupCode$FSI_0008>.$FSI_0008.main@()
Stopped due to error

Saturday, March 16, 2013

Active Pattern (3) : Parameterized Active Pattern

(In learning from "Programming F# 3.0, 2nd Edition")

Parameterized Active Pattern takes both condition to match and parameter of its condition, and also used with Partial Active Pattern as "Parameterized Partial Active Pattern".

let (|MultiplesOf|_|) (multiplier : int) (input : int) =
    if (input % multiplier = 0) then Some(input) else None

let fizzBuzz input =
    match input with
    | MultiplesOf 5 _ & MultiplesOf 3 _ -> "FizzBuzz"
    | MultiplesOf 5 _ -> "Buzz"
    | MultiplesOf 3 _ -> "Fizz"
    | _ -> input.ToString()

List.map fizzBuzz [1..40];;

Thursday, March 14, 2013

Active Pattern (2) : Partial Active Pattern

(In learning from "Programming F# 3.0, 2nd Edition")

If your "Single-case Active Pattern" should be considered for dealing option type (e.g. phosphorescence: Active Pattern (1) : Single-case Active Pattern: this sample is NOT considering invalid date), you should define as "Partial Active Pattern" like below:
> open System;;
> let (|WhatDayOfWeek|_|) (year, month, day) =
-     try
-         Some(System.DateTime(year,month,day).DayOfWeek)
-     with
-     | :? System.ArgumentOutOfRangeException -> None;;

val ( |WhatDayOfWeek|_| ) : year:int * month:int * day:int -> DayOfWeek option

> let weekEnd year month day =
-     match (year, month, day) with
-     | WhatDayOfWeek System.DayOfWeek.Sunday
-     | WhatDayOfWeek System.DayOfWeek.Saturday
-         -> "Week End !!"
-     | WhatDayOfWeek _
-         -> "Not Week End..."
-     | _ -> "invalid date";;

val weekEnd : year:int -> month:int -> day:int -> string

> weekEnd 2013 3 15;;
val it : string = "Not Week End..."
> weekEnd 2013 3 16;;
val it : string = "Week End !!"
> weekEnd 2013 2 29;;
val it : string = "invalid date"

Monday, March 11, 2013

Active Pattern (1) : Single-case Active Pattern

(In learning from "Programming F# 3.0, 2nd Edition")

Single-case Active Pattern is defined as a special function enclosed (| |).
> open System;;
> let (|WhatDayOfWeek|) (year, month, day) =
-     System.DateTime(year,month,day).DayOfWeek;;

val ( |WhatDayOfWeek| ) : year:int * month:int * day:int -> DayOfWeek

> let isWeekEnd year month day =
-     match (year, month, day) with
-     | WhatDayOfWeek System.DayOfWeek.Sunday
-     | WhatDayOfWeek System.DayOfWeek.Saturday
-         -> true
-     | WhatDayOfWeek _
-         -> false;;

val isWeekEnd : year:int -> month:int -> day:int -> bool

> isWeekEnd 2013 3 15;;
val it : bool = false
> isWeekEnd 2013 3 16;;
val it : bool = true

Friday, March 8, 2013

pkgng on PC-BSD rolling release is not stable.

The package management tool of PC-BSD rolling release is pkg(8)(a.k.a. pkgng). But, IMHO, this is not stable yet.
  • XScreensaver for pkgng is 5.12, but default version of PC-BSD rolling release is 5.20. When executing pkg upgrade, Xscreensaver and its releated GUI libraries are degraded.
  • As written in this manual, when accepting to use pkgng, it is NOT reversible to any existing package management tools.
So I decide not to use pkgng for a while, until FreeBSD/PC-BSD 10.0 will be released.

(/etc/make.conf)
WITHOUT_PKGNG=yes

Tuesday, March 5, 2013

Saturday, March 2, 2013

Dispose pattern in F#

(In learning from "Programming F# 3.0, 2nd Edition")

If we want to access to unmanaged resources from C#,
  1. The class that indicates unmanaged resource should implement IDisposable interface and override Dispose method.
  2. When using this class, we should write the code with using () {} clause.

In F#, almost same.
  1. The class that indicates unmanaged resource should implement IDisposable interface and override Dispose method.
  2. When using this class, we should write the code with use binding.
> open System;;
> type SomeUnmanagedResource() =
-     interface IDisposable with
-         member this.Dispose() =
-             printfn "Some unmanaged resource is diposed.";;

type SomeUnmanagedResource =
  class
    interface IDisposable
    new : unit -> SomeUnmanagedResource
  end

> let greetings =
-     use res = new SomeUnmanagedResource()
-     printfn "Hello World";;
Hello World
Some unmanaged resource is diposed.

val greetings : unit = ()