Created
December 4, 2011 18:42
-
-
Save dmohl/1430954 to your computer and use it in GitHub Desktop.
UndoMonad Calculator Example
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 | |
open FSharpx.Undo | |
let add x = undoable { | |
let! currentVal = getCurrent | |
do! putToHistory (currentVal + x) | |
return currentVal} | |
let undo = undoable { | |
let! _ = undo | |
return! getCurrent} | |
let rec handleInput calculator input = | |
let notifyAndContinue result = | |
printfn "The current total is %O" (result |> current) | |
Console.ReadLine() |> handleInput result | |
match input with | |
| "undo" -> undo calculator | |
| _ -> let _, x = Double.TryParse(input) | |
add x calculator | |
|> snd |> notifyAndContinue | |
Console.ReadLine() |> handleInput (empty 0.) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment