Created
March 25, 2017 13:58
-
-
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
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
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