Mac OS X で F#

MacPorts から F# を入れて触ってみました.

インストール

sudo port install fsharp

インタプリタの起動

fsi

しばらく待つと mono がむっくりと起き上がって,プロンプトがでます。
もし起動しない場合は, bashrc に

export DYLD_FALLBACK_LIBRARY_PATH=/usr/lib:/opt/local/lib

などとすればよいでしょう。 もちろん OCaml と findlib を使う場合は

export DYLD_FALLBACK_LIBRARY_PATH=/usr/lib:/opt/local/lib:/opt/local/lib/ocaml/site-lib/stublibs

です.

$ fsi
MSR F# Interactive, (c) Microsoft Corporation, All Rights Reserved
F# Version 1.9.4.19, compiling for .NET Framework Version v2.0.50727

NOTE: 
NOTE: See 'fsi --help' for flags
NOTE: 
NOTE: Commands: #r <string>;;    reference (dynamically load) the given DLL. 
NOTE:           #I <string>;;    add the given search path for referenced DLLs. 
NOTE:           #use <string>;;  accept input from the given file. 
NOTE:           #load <string> ...<string>;;
NOTE:                            load the given file(s) as a compilation unit.
NOTE:           #time;;          toggle timing on/off. 
NOTE:           #types;;         toggle display of types on/off. 
NOTE:           #quit;;          exit. 
NOTE: 
NOTE: Visit the F# website at http://research.microsoft.com/fsharp.
NOTE: Bug reports to fsbugs@microsoft.com. Enjoy!

> 1+1;;
val it : int = 2
> List.map (fun x -> x+1) [1;2;3;4;5];;
val it : int list = [2; 3; 4; 5; 6]
> #quit;;

- Exit...
$

とりあえず List.mapは使えた。やや動きはもっさりしています。

コンパイルと実行

F#のファイルの拡張子は .fs らしい.

test.fs
print_string "Hello, World!"

コンパイル

コンパイラfsc.exe ではなく fscp (.NET 2.0以降?) または fscp10 (.NET 1.0系?) を使うようだ. (あとなんか fsc と打つと Scalaコンパイラが起動したでござる)

$ fscp test.fs

test.fs(1,0): warning FS0062: This construct is for compatibility with OCaml. Consider using 'System.Console.Write(string)' instead. This warning can be disabled using '--ml-compatibility', '--no-warn 62' or '#nowarn "62"'
$

print_string ではなく Syste.Console.Write を使えと警告される.このあたりはまだよくわからない.
できあがったのは Windows のバイナリっぽく見えてぎょっとする.

$ file test.exe
test.exe: MS-DOS executable PE  for MS Windows (console) Intel 80386 32-bit

実行

monoから叩く.

$ mono test.exe
Hello, World!$