Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- open System
- let rec parseAndCalcCore stack tokens =
- match tokens, stack with
- | [], [single] -> single
- | "+"::rest, h1::h2::tail -> parseAndCalcCore (h2 + h1::tail) rest
- | "-"::rest, h1::h2::tail -> parseAndCalcCore (h2 - h1::tail) rest
- | "/"::rest, h1::h2::tail -> parseAndCalcCore (h2 / h1::tail) rest
- | "*"::rest, h1::h2::tail -> parseAndCalcCore (h2 * h1::tail) rest
- | h::rest, _ -> parseAndCalcCore (((h |> double)::stack)) rest
- | _ -> failwith "The stack does not contains needed elements"
- let parseAndCalc (expr : string) =
- expr.Split ' '
- |> Array.toList
- |> parseAndCalcCore []
- |> string
- let print = parseAndCalc >> Console.WriteLine
- [<EntryPoint>]
- let main argv =
- print "1 1 +"
- print "2 -3 *"
- print "1 1 + 2 3 * -"
- print "1 3 / 2 *"
- print "10 1 2 + 2 + 5 + +"
- 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement