Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- open System
- let doAdd (stack : double list) : double list =
- stack.Tail.Head + stack.Head :: stack.Tail.Tail
- let doSub (stack : double list) : double list =
- stack.Tail.Head - stack.Head :: stack.Tail.Tail
- let doDiv (stack : double list) : double list =
- stack.Tail.Head / stack.Head :: stack.Tail.Tail
- let doMul (stack : double list) : double list =
- stack.Tail.Head * stack.Head :: stack.Tail.Tail
- let rec parseAndCalcCore (tokens : string list) (stack : double list) : double =
- match tokens with
- | [] -> stack.Head
- | h::t -> match h with
- | "+" -> doAdd stack |> parseAndCalcCore t
- | "-" -> doSub stack |> parseAndCalcCore t
- | "/" -> doDiv stack |> parseAndCalcCore t
- | "*" -> doMul stack |> parseAndCalcCore t
- | _ -> ((h |> double) :: stack) |> parseAndCalcCore t
- let parseAndCalc (expr : string) : string =
- let tokens = (expr.Split ' ' |> Array.toList)
- parseAndCalcCore tokens [] |> string
- [<EntryPoint>]
- let main argv =
- Console.WriteLine (parseAndCalc "1 1 +")
- Console.WriteLine (parseAndCalc "2 -3 *")
- Console.WriteLine (parseAndCalc "1 1 + 2 3 * -")
- Console.WriteLine (parseAndCalc "1 3 / 2 *")
- Console.WriteLine (parseAndCalc "10 1 2 + 2 + 5 + +")
- 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement