Given a string containing brackets []
, braces {}
, parentheses ()
,
or any combination thereof, verify that any and all pairs are matched
and nested correctly.
For library documentation, follow Useful OCaml resources.
A Makefile
is provided with a default target to compile your solution and run the tests. At the command line, type:
make
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
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!
Ginna Baker
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)
open Base
exception NoPairFound
let r_to_l = function
| ']' -> '['
| ')' -> '('
| '}' -> '{'
| _ -> raise NoPairFound
let hd_equal list char =
match List.hd list with
| None -> false
| Some hd -> Char.equal hd char
let always_tl list =
match List.tl list with
| None -> []
| Some tl -> tl
let are_balanced =
let f acc el =
match el with
| ']' | ')' | '}' ->
if hd_equal acc (r_to_l el)
then Continue_or_stop.Continue (always_tl acc)
else Continue_or_stop.Stop false
| '[' | '(' | '{' ->
Continue_or_stop.Continue (el::acc)
| _ ->
Continue_or_stop.Continue acc
in
String.fold_until ~init:[] ~f ~finish:List.is_empty
A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.
Here are some questions to help you reflect on this solution and learn the most from it.
Level up your programming skills with 3,393 exercises across 50 languages, and insightful discussion with our volunteer team of welcoming mentors. Exercism is 100% free forever.
Sign up Learn More
Community comments