Skip to content

Instantly share code, notes, and snippets.

@dmohl
Created December 4, 2011 18:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dmohl/1430954 to your computer and use it in GitHub Desktop.
Save dmohl/1430954 to your computer and use it in GitHub Desktop.
UndoMonad Calculator Example
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