LeetCode 1106. Parsing A Boolean Expression in F#

URL

https://leetcode.com/problems/parsing-a-boolean-expression/description/?envType=daily-question&envId=2024-10-20

Code

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

let parseBoolExpr (expression: string) : bool =
    let rec parseBoolExpr' cs =
        match cs with
        | [] -> true, []
        | 't' :: t -> true, t
        | 'f' :: t -> false, t
        | '!' :: '(' :: t ->
            let v, cs = parseBoolExpr' t
            not v, List.tail cs
        | '&' :: '(' :: t ->
            let exprs, t = parseExprs t []
            List.reduce (&&) exprs, t
        | '|' :: '(' :: t ->
            let exprs, t = parseExprs t []
            List.reduce (||) exprs, t
        | _ -> failwith "never reach here"

    and parseExprs cs acc =
        match cs with
        | [] -> failwith ") is not found"
        | ')' :: t -> acc, t
        | ',' :: t -> parseExprs t acc
        | _ ->
            let v, t = parseBoolExpr' cs
            parseExprs t (v :: acc)

    parseBoolExpr' (Seq.toList expression) |> fst