F#とOCamlの違い
F# at Microsoft Research - Microsoft ResearchについてきたTutorial.fsを読みながら、F#とOCamlの違いついてメモしてみます。
拡張子が.fs
OCamlは.mlだけど、F#は.fs。
コメントが//
OCamlのコメントは(* ... *)だけど、F#は//。
範囲
let oneToTen = [1..10]
内包表記
/// The squares of the first 10 integers let squaresOfOneToTen = [ for x in 0..10 -> x*x ]
ディクショナリ
let lookupTable = dict [ (1, "One"); (2, "Two") ] let oneString = lookupTable.[1]
パイプライン
2008-11-12の+>と同じですね。
let squares3 = [1; 2; 3; 4] |> List.map (fun x -> x*x)
@@と同じの<|もある。
fact <| 1 + 2
型やフィールドが大文字で始まる
OCamlでこれって許されていたかな?どっちにしろ、違和感がある。
type Card = { Name : string; Phone : string; Ok : bool }
クラスのインタフェース
お、だんだんずれてきたぞ。
// Types: classes // --------------------------------------------------------------- /// A 2-dimensional vector type Vector2D(dx:float, dy:float) = // The pre-computed length of the vector let length = sqrt(dx*dx + dy*dy) /// The displacement along the X-axis member v.DX = dx /// The displacement along the Y-axis member v.DY = dy /// The length of the vector member v.Length = length // Re-scale the vector by a constant member v.Scale(k) = Vector2D(k*dx, k*dy) // Types: interfaces // --------------------------------------------------------------- type IPeekPoke = abstract Peek: unit -> int abstract Poke: int -> unit
// Print a result using %A for generic printing printfn "listC = %A" listC