Skip to content

Instantly share code, notes, and snippets.

@medigor
Last active September 25, 2019 19:43
Show Gist options
  • Save medigor/f7438dbe834957457c6df88083400315 to your computer and use it in GitHub Desktop.
Save medigor/f7438dbe834957457c6df88083400315 to your computer and use it in GitHub Desktop.
let validate (str: string) =
let rec loop (i: int) (acc: char list) =
if i >= str.Length then
acc.IsEmpty
else
let validateChar ch =
not acc.IsEmpty && acc.Head = ch && loop (i + 1) acc.Tail
match str.[i] with
| '(' | '[' | '{' -> loop (i + 1) (str.[i]::acc)
| ')' -> validateChar '('
| ']' -> validateChar '['
| '}' -> validateChar '{'
| _ -> loop (i + 1) acc
loop 0 []
[<EntryPoint>]
let main argv =
assert validate "(){()()}{}[]"
assert (not <| validate "(]")
assert (not <| validate ")")
0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment