ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

# LiamGoodacre's solution

## to Bracket Push in the PureScript Track

Published at Aug 02 2018 · 0 comments
Instructions
Test suite
Solution

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

Ginna Baker

## Submitting Incomplete Solutions

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

### Main.purs

module Test.Main where

import Prelude

import Effect (Effect)
import Test.Unit.Assert as Assert
import Test.Unit (TestSuite, suite, test)
import Test.Unit.Main (runTest)
import BracketPush (isPaired)

main :: Effect Unit
main = runTest suites

suites :: TestSuite
suites = do
suite "BracketPush.isPaired" do

test "paired square brackets" $Assert.equal true (isPaired "[]") test "empty string"$
Assert.equal true
(isPaired "")

test "unpaired brackets" $Assert.equal false (isPaired "[[") test "wrong ordered brackets"$
Assert.equal false
(isPaired "}{")

test "paired with whitespace" $Assert.equal true (isPaired "{ }") test "simple nested brackets"$
Assert.equal true
(isPaired "{[]}")

test "several paired brackets" $Assert.equal true (isPaired "{}[]") test "paired and nested brackets"$
Assert.equal true
(isPaired "([{}({}[])])")

test "unopened closing brackets" $Assert.equal false (isPaired "{[)][]}") test "unpaired and nested brackets"$
Assert.equal false
(isPaired "([{])")

test "paired and wrong nested brackets" $Assert.equal false (isPaired "[({]})") test "math expression"$
Assert.equal true
(isPaired "(((185 + 223.85) * 15) - 543)/2")

test "complex latex expression" $Assert.equal true (isPaired "\\left(\\begin{array}{cc} \\frac{1}{3} & x\\\\ \\mathrm{e}^{x} &... x^2 \\end{array}\\right)") module BracketPush ( isPaired ) where import Prelude import Data.Array as Array import Data.List (List(..), uncons) import Data.Maybe (Maybe(..)) import Data.String as String import Control.Alt ((<|>)) import Control.MonadZero (guard) data Bracket = Paren | Curley | Square derive instance eqBracket :: Eq Bracket parseAtom :: forall r . r -> Char -> String.CodePoint -> Maybe r parseAtom r c cp = if String.codePointFromChar c == cp then Just r else Nothing parseOpenBracket :: String.CodePoint -> Maybe Bracket parseOpenBracket cp = parseAtom Paren '(' cp <|> parseAtom Curley '{' cp <|> parseAtom Square '[' cp parseCloseBracket :: String.CodePoint -> Maybe Bracket parseCloseBracket cp = parseAtom Paren ')' cp <|> parseAtom Curley '}' cp <|> parseAtom Square ']' cp stepParser :: List Bracket -> String.CodePoint -> Maybe (List Bracket) stepParser stack cp | Just bracket <- parseOpenBracket cp = Just (Cons bracket stack) stepParser stack cp | Just bracket <- parseCloseBracket cp = do {head, tail} <- uncons stack guard (head == bracket)$> tail
stepParser stack _ =
Just stack

isPaired :: String -> Boolean
isPaired =
(Just Nil == _) <<<
Array.foldRecM stepParser Nil <<<
String.toCodePointArray