Skip to content

Instantly share code, notes, and snippets.

@jdh30
Created March 25, 2017 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jdh30/613a7eccaf22aaed381e3f1544deea24 to your computer and use it in GitHub Desktop.
Save jdh30/613a7eccaf22aaed381e3f1544deea24 to your computer and use it in GitHub Desktop.
Reverse polish calculator in F#, derived from this 35-line Racket metaprogramming example http://beautifulracket.com/stacker/source-listing.html
let (|Float|_|) s =
let mutable x = 0.0
if System.Double.TryParse(s, &x) then Some x else None
let binop f x y stack =
let z = f x y
printfn "%A" z
z::stack
let rec loop stack =
match stdin.ReadLine(), stack with
| Float x, stack -> x::stack
| "+", x::y::stack -> binop (+) x y stack
| "*", x::y::stack -> binop (*) x y stack
| _ -> stack
|> loop
do loop[]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment