Avatar of shmibs

shmibs's solution

to Matching Brackets in the OCaml Track

Published at Dec 09 2018 · 0 comments
Test suite


This exercise has changed since this solution was written.

Bracket Push

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

Getting Started

For installation and learning resources, refer to the exercism help page.


To work on the exercises, you will need Opam and Base. Consult opam website for instructions on how to install opam for your OS. Once opam is installed open a terminal window and run the following command to install base:

opam install base

To run the tests you will need OUnit. Install it using opam:

opam install ounit

Running Tests

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


Interactive Shell

utop is a command line program which allows you to run Ocaml code interactively. The easiest way to install it is via opam:

opam install utop

Consult utop for more detail.

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. We'll do our best to help you!


Ginna Baker

Submitting Incomplete Solutions

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


open Base
open OUnit2
open Bracket_push

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 "[({]})");
  "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 ("bracket-push tests" >::: tests)
open Core

module CS = Base.Continue_or_stop

type bracket = Square | Brace | Paren

let pop acc b =
  match acc with
  | h :: t -> if h = b then CS.Continue t else CS.Stop false
  | _ -> CS.Stop false

let push_or_pop acc c =
  match c with
  | '[' -> CS.Continue (Square :: acc)
  | '{' -> CS.Continue (Brace :: acc)
  | '(' -> CS.Continue (Paren :: acc)
  | ']' -> pop acc Square
  | '}' -> pop acc Brace
  | ')' -> pop acc Paren
  | _ -> CS.Continue acc

let are_balanced (s : string) =
  String.fold_until s ~init:[] ~f:push_or_pop ~finish:List.is_empty

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

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.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?