Skip to content

Instantly share code, notes, and snippets.

@liammclennan
Last active August 29, 2015 13:59
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 liammclennan/10521441 to your computer and use it in GitHub Desktop.
Save liammclennan/10521441 to your computer and use it in GitHub Desktop.
Reverse Polish notation
let solveRPN expression =
let foldingFunction stack item =
match (stack,item) with
| (x::y::xs,"*") -> x * y :: xs
| (x::y::xs,"+") -> x + y :: xs
| (x::y::xs,"-") -> y - x :: xs
| (s,o) -> int o :: s
let words (i:string) = i.Split(' ') |> List.ofArray
words expression
|> List.fold foldingFunction []
|> List.head
solveRPN "10 4 3 + 2 * -" |> Console.WriteLine
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment