Skip to content

Instantly share code, notes, and snippets.

@jdh30
Created March 26, 2017 00:18
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/aac85c505773ea44e8449d43837aebb1 to your computer and use it in GitHub Desktop.
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
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