# foocraft's solution

## to Matching Brackets in the OCaml Track

Published at May 06 2020 · 0 comments
Instructions
Test suite
Solution

Given a string containing brackets [], braces {}, parentheses (), or any combination thereof, verify that any and all pairs are matched and nested correctly.

## Getting Started

1. For library documentation, follow Useful OCaml resources.

## Running Tests

A Makefile is provided with a default target to compile your solution and run the tests. At the command line, type:

make


## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

## Feedback, Issues, Pull Requests

The exercism/ocaml repository on GitHub is the home for all of the Ocaml exercises.

If you have feedback about an exercise, or want to help implementing a new one, head over there and create an issue or submit a PR. We welcome new contributors!

## Source

Ginna Baker

### test.ml

open Base
open OUnit2
open Matching_brackets

let ae exp got _test_ctxt =
assert_equal exp got ~printer:Bool.to_string

let tests = [
"paired square brackets" >::
ae true (are_balanced "[]");
"empty string" >::
ae true (are_balanced "");
"unpaired brackets" >::
ae false (are_balanced "[[");
"wrong ordered brackets" >::
ae false (are_balanced "}{");
"wrong closing bracket" >::
ae false (are_balanced "{]");
"paired with whitespace" >::
ae true (are_balanced "{ }");
"partially paired brackets" >::
ae false (are_balanced "{[])");
"simple nested brackets" >::
ae true (are_balanced "{[]}");
"several paired brackets" >::
ae true (are_balanced "{}[]");
"paired and nested brackets" >::
ae true (are_balanced "([{}({}[])])");
"unopened closing brackets" >::
ae false (are_balanced "{[)][]}");
"unpaired and nested brackets" >::
ae false (are_balanced "([{])");
"paired and wrong nested brackets" >::
ae false (are_balanced "[({]})");
"paired and incomplete brackets" >::
ae false (are_balanced "{}[");
"too many closing brackets" >::
ae false (are_balanced "[]]");
"math expression" >::
ae true (are_balanced "(((185 + 223.85) * 15) - 543)/2");
"complex latex expression" >::
ae true (are_balanced "\\left(\\begin{array}{cc} \\frac{1}{3} & x\\\\ \\mathrm{e}^{x} &... x^2 \\end{array}\\right)");
]

let () =
run_test_tt_main ("matching-brackets tests" >::: tests)
let rec list_contains x = function
| x' :: xs -> x' = x || (list_contains x xs)
| [] -> false

let are_balanced s =
let opening = ['['; '{'; '('] in
let closing = [']'; '}'; ')'] in
let check_next stack c = Option.bind stack (fun stack ->
if list_contains c opening then
(Stack.push c stack; Some(stack))
else if not (list_contains c opening || list_contains c closing) then
Some(stack)
else if c = ']' && Stack.top stack = '[' then
let _ = Stack.pop stack in Some(stack)
else if c = ')' && Stack.top stack = '(' then
let _ = Stack.pop stack in Some(stack)
else if c = '}' && Stack.top stack = '{' then
let _ = Stack.pop stack in  Some(stack)
else None
) in
try
match String.to_seq s |> Seq.fold_left check_next (Some(Stack.create())) with
| Some(s) -> Stack.is_empty s
| None -> false
with Stack.Empty -> false