Avatar of DougVonMoser

DougVonMoser's solution

to Matching Brackets in the Elm Track

Published at Jun 27 2019 · 0 comments
Instructions
Test suite
Solution

Note:

This exercise has changed since this solution was written.

Bracket Push

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

Elm Installation

Refer to the Installing Elm page for information about installing elm.

Writing the Code

The first time you start an exercise, you'll need to ensure you have the appropriate dependencies installed. Thankfully, Elm makes that easy for you and will install dependencies when you try to run tests or build the code.

Execute the tests with:

$ elm-test

Automatically run tests again when you save changes:

$ elm-test --watch

As you work your way through the test suite, be sure to remove the skip <| calls from each test until you get them all passing!

Source

Ginna Baker

Submitting Incomplete Solutions

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

Tests.elm

module Tests exposing (tests)

import BracketPush exposing (isPaired)
import Expect
import String
import Test exposing (..)


tests : Test
tests =
    describe "BracketPush"
        [ test "paired square brackets" <|
            \_ ->
                Expect.equal True (isPaired "[]")
        , skip <|
            test "empty string" <|
                \_ ->
                    Expect.equal True (isPaired "")
        , skip <|
            test "unpaired brackets" <|
                \_ ->
                    Expect.equal False (isPaired "[[")
        , skip <|
            test "wrong ordered brackets" <|
                \_ ->
                    Expect.equal False (isPaired "}{")
        , skip <|
            test "wrong closing bracket" <|
                \_ ->
                    Expect.equal False (isPaired "{]")
        , skip <|
            test "paired with whitespace" <|
                \_ ->
                    Expect.equal True (isPaired "{ }")
        , skip <|
            test "partially paired brackets" <|
                \_ ->
                    Expect.equal False (isPaired "{[])")
        , skip <|
            test "simple nested brackets" <|
                \_ ->
                    Expect.equal True (isPaired "{[]}")
        , skip <|
            test "seceral paired brackets" <|
                \_ ->
                    Expect.equal True (isPaired "{}[]")
        , skip <|
            test "paired and nested brackets" <|
                \_ ->
                    Expect.equal True (isPaired "([{}({}[])])")
        , skip <|
            test "unopened closing brackets" <|
                \_ ->
                    Expect.equal False (isPaired "{[)][]}")
        , skip <|
            test "unpaired and nested brackets" <|
                \_ ->
                    Expect.equal False (isPaired "([{])")
        , skip <|
            test "paired and wrong nested brackets" <|
                \_ ->
                    Expect.equal False (isPaired "[({]})")
        , skip <|
            test "math expression" <|
                \_ ->
                    Expect.equal True (isPaired "(((185 + 223.85) * 15) - 543)/2")
        , skip <|
            test "complex latex expression" <|
                \_ ->
                    Expect.equal True (isPaired "\\left(\\begin{array}{cc} \\frac{1}{3} & x\\\\ \\mathrm{e}^{x} &... x^2 \\end{array}\\right)")
        ]
module BracketPush exposing (isPaired)


type Bracket
    = Open Delimeter
    | Closed Delimeter


type Delimeter
    = Square
    | Paren
    | Curly


isPaired : String -> Bool
isPaired input =
    helper [] input


helper : List Bracket -> String -> Bool
helper open rest =
    case String.uncons rest of
        Just ( current, remainingInput ) ->
            case decodeCurrent current of
                Just ((Open _) as openDelimeter) ->
                    helper (openDelimeter :: open) remainingInput

                Just ((Closed _) as closedDelimeter) ->
                    case compareClosed open closedDelimeter of
                        PairedAndHeresTheRest remaining ->
                            helper remaining remainingInput

                        DidNotPairAndBail ->
                            False

                Nothing ->
                    helper open remainingInput

        Nothing ->
            List.length open == 0


type DidTheyPair
    = PairedAndHeresTheRest (List Bracket)
    | DidNotPairAndBail


compareClosed : List Bracket -> Bracket -> DidTheyPair
compareClosed open current =
    case open of
        couldBe :: restOpened ->
            case doesClose couldBe current of
                True ->
                    PairedAndHeresTheRest restOpened

                False ->
                    DidNotPairAndBail

        [] ->
            DidNotPairAndBail


doesClose : Bracket -> Bracket -> Bool
doesClose head close =
    case ( head, close ) of
        ( Open openDelimeter, Closed closedDelimeter ) ->
            openDelimeter == closedDelimeter

        _ ->
            False


decodeCurrent : Char -> Maybe Bracket
decodeCurrent char =
    case char of
        '[' ->
            Just (Open Square)

        ']' ->
            Just (Closed Square)

        '(' ->
            Just (Open Paren)

        ')' ->
            Just (Closed Paren)

        '{' ->
            Just (Open Curly)

        '}' ->
            Just (Closed Curly)

        _ ->
            Nothing

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?