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];;