LeetCode 150. Evaluate Reverse Polish Notation in F#

URL

Evaluate Reverse Polish Notation - LeetCode

Code

https://github.com/syohex/dotnet-study/tree/master/fsharp/leetcode/challenge/202401/evaluate_reverse_polish_notation/main.fsx

let evalRPN (tokens: string list) : int =
    let rec evalRPN' tokens stack =
        match tokens with
        | [] -> List.head stack
        | h :: t ->
            match h with
            | "+" ->
                let num1, num2 = List.head (List.tail stack), List.head stack
                evalRPN' t ((num1 + num2) :: stack)
            | "-" ->
                let num1, num2 = List.head (List.tail stack), List.head stack
                evalRPN' t ((num1 - num2) :: stack)
            | "*" ->
                let num1, num2 = List.head (List.tail stack), List.head stack
                evalRPN' t ((num1 * num2) :: stack)
            | "/" ->
                let num1, num2 = List.head (List.tail stack), List.head stack
                evalRPN' t ((num1 / num2) :: stack)
            | _ -> evalRPN' t ((int h) :: stack)

    evalRPN' tokens []