Last active
December 17, 2015 08:39
-
-
Save Gab-km/5582054 to your computer and use it in GitHub Desktop.
人様の作品をいじっているだけです
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open System | |
//============================================ | |
// IOモナド | |
//============================================ | |
module IO = | |
type IO<'a> = IO of (unit -> 'a) | |
type IOBuilder() = | |
member this.Bind (IO x, f) = f (x()) | |
member this.Return x = x | |
member this.Delay (f : unit -> 'a) = IO f | |
let io = new IOBuilder() | |
let run (IO i) = i() | |
let read = io { return Console.ReadLine() } | |
let putStr str = io { return printf "%s" str } | |
let putStrn str = io { return printf "%s\n" str } | |
let waitKey = io { return Console.ReadKey true |> ignore } | |
open IO | |
//============================================ | |
// エントリポイント | |
//============================================ | |
let mainIO = | |
io { | |
do! putStrn "Input your name." | |
let! name = read | |
do! putStrn ("Hello " + name) | |
do! waitKey | |
} | |
[<EntryPoint>] | |
let main argv = | |
run mainIO | |
0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment