FSharp

ファイルの順番の話

こーいう F# のソースファイルがありますよね。 Batteries.Env モジュールと: // env.fs module Batteries open System module Env = let at (index : int) = let argv = Environment.GetCommandLineArgs() argv.[index] let get = at それを参照する main.f…

Argu すごい

Argu は F# 製のコマンドラインパーサーだよ。 gmake のオプションを前半だけ書いてみた。 type Arguments = | [<AltCommandLine("-B")>] Always_Make | [<AltCommandLine("-C")>] Directory of dir : string | Debug of mode : DebugMode option | [<AltCommandLine("-e")>] Environment_Overrides | Eval of expr : string | [<AltCommandLine("-f")></altcommandline("-f")></altcommandline("-e")></altcommandline("-c")></altcommandline("-b")>…

printfn とかで出力する時に

最近までこんな風にやってたんだけど: let name = user.GetName() // printfn "Hello, %s!" user.GetName() はエラーになっちゃうので // 一旦、変数に束縛する。 printfn "Hello, %s!" name 別にそんなことをする必要はなかったらしい。 <| を使うんだルー…

ちょっと休憩

というか、めちゃくちゃ進んでしまったので、書くのめんどくさい。 % nuget install FParsec -SolutionDirectory . とやることで、./packages/ に FParsec がインストールされることがわかった。

FParsec のチュートリアル的な何か

さて、今日は FParsec 日本語チュートリアルを読んでやってみるよ。 nuget はインストールしてるかな? 私は nuget3 をインストールしてみたよ: % yaourt -S nuget3 勉強用のディレクトリを作成しよう: % mkdir ./hello-fparsec % cd ./hello-fparsec FParse…

OCaml の Test::Simple を目コピしてみた その 4

OCaml のソースコードに沿って TAPDocument.ml を test-document.fs にして、TAPBuilder.ml を test-builder.fs に書いてみた。 なんかクラスっぽいやつでやってみたんだけど、これでいいのかな…………(汗 module Test.Builder type Plan(?count : int) = let c…

OCaml の Test::Simple を目コピしてみた その 3

続き。 ʕ•͡ω•ʔ TAPDocument.ml 完全目コピでけたー! init_document はなんか初期化しそう。 let init_document doc = let count = count_tests doc in let failures = count_test_failures doc in match doc with | Document(PlanNode(plan)::nodes) -> Doc…

OCaml の Test::Simple を目コピしてみた その 2

続き。 OCaml では文字列の結合は ^ だけど*1、F# では + だ。 また、OCaml での string_of_int は F# には存在しないけど、書くとしたらこんな感じになる: let string_of_int (n:int):string = n.ToString() DiagnosticNode を返す関数 2 連発: let create_…

OCaml の Test::Simple を目コピしてみた

これ。 普通に OCaml のコードがコンパイルできるのすごい。 モジュール名は Test.Builder にしておく。 module Test.Builder type status = | NotOk | Ok type directive = | Todo of string type diagnostic = | Diag of string list type node = | TestCa…

F# で Either もできたよー

ʕ•͡ω•ʔ 最近 fsharp-mode より tuareg-mode のがいいんじゃないかって思うようになってきた module Data.Either type either<'a, 'b> = | Left of 'a | Right of 'b let either f g e = match e with | Left a -> f a | Right b -> g b let isLeft x = match…

F# で Maybe できたよーーー

ʕ•͡ω•ʔ Haskell の何かを F# で書くと楽しいんじゃ(天才博士風) というわけで、実装して覚えるシリーズ。 型の定義がこちら: type Maybe<'a> = | Nothing | Just of 'a F# 風にするなら、Maybe を maybe にするべきかも。 ゆかいな Maybe 関連の関数: let ma…

FSharp やってみた2

その 2。 add という関数を定義して、引数を渡して評価してみる: > let add x y = - x + y;; val add : x:int -> y:int -> int > add 2 2;; val it : int = 4 関数のボディ部分を折ってるけど、インデントしないと Warning が出るので注意。 あと、終了する…

FSharp やってみた

良い感じのブラウザだと Try F# で式を評価できるらしいんだけど、できないブラウザだったので、処理系をインストールして式を評価してみるよ。 インストール yaourt で fsharp をインストールできるよ。 % yaourt -S fsharp REPL 起動 REPL を起動する時は …