Created
March 26, 2017 00:18
-
-
Save jdh30/aac85c505773ea44e8449d43837aebb1 to your computer and use it in GitHub Desktop.
Reverse polish calculator compiler in F# using quotations, derived from this 35-line Racket example http://beautifulracket.com/stacker/source-listing.html Raw
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 rec (|Parse|) = function | |
| Float x::t -> <@ x @>, t | |
| "+"::Parse(x, Parse(y, t)) -> <@ %x + %y @>, t | |
| "*"::Parse(x, Parse(y, t)) -> <@ %x * %y @>, t | |
let (Parse(f, _)) = stdin.ReadToEnd().Split '\n' |> List.ofSeq |> List.rev | |
printfn "%f" (QuotationCompiler.QuotationCompiler.Eval f) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment