Last active
August 29, 2015 13:59
-
-
Save liammclennan/10521441 to your computer and use it in GitHub Desktop.
Reverse Polish notation
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 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