Detecting properly nested parenthesis using functional programming
The algorithm is overly complicated, and incorrect: for example, it will fail on the input (())().


  I feel like it could ... use a more functional approach to solving the problem.


There is a more functional approach, maintaining a count of how many parentheses are open.

Let's start with the function signature

let rec isNested' (parens : char list) (openParens : int) : bool =


And fill in the cases. If there are no parentheses left to process, and no open parentheses, they are properly nested.

    match parens, openParens with
    | [], 0 -&gt; true


If we are processing a (, we increase the open parentheses count

    | '(' :: rest, _ -&gt; isNested' rest (openParens + 1)


I'll leave the other two cases up to you.

For convenience, we add a wrapper for this function

let isNested (parens : string) = isNested' (List.ofSeq parens) 0