🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉 # nganhkhoa's solution

## to Bracket Push in the PureScript Track

Published at Oct 30 2019 · 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.Maybe (Maybe(..), fromJust) import Data.List (List(..), (:), foldl, length, head, tail, snoc, insertAt) import Data.String.CodeUnits (toCharArray) import Partial.Unsafe (unsafePartial) data BracketType = Round | Square | Curly derive instance eqBracketType :: Eq BracketType type Bracket = { type :: BracketType, enclosed :: Boolean } parseChar :: Char -> Maybe Bracket parseChar c | c == '{' = Just { type: Curly, enclosed: false } | c == '(' = Just { type: Round, enclosed: false } | c == '[' = Just { type: Square, enclosed: false } | c == ']' = Just { type: Square, enclosed: true } | c == ')' = Just { type: Round, enclosed: true } | c == '}' = Just { type: Curly, enclosed: true } | otherwise = Nothing stackBracket :: Maybe (List Bracket) -> Maybe Bracket -> Maybe (List Bracket) stackBracket acc (Nothing) = acc stackBracket (Nothing) (Just c) = Just (c : Nil) stackBracket (Just acc) (Just c@{type: _, enclosed: false}) = insertAt 0 c acc stackBracket (Just acc) (Just c@{type: _, enclosed: true}) = if (checkHead c listHead) then tail acc else Just (snoc acc (unsafePartial (fromJust listHead))) where listHead :: Maybe Bracket listHead = head acc checkHead :: Bracket -> Maybe Bracket -> Boolean checkHead ch (Just hd) = ch.type == hd.type checkHead _ _ = false isPaired :: String -> Boolean isPaired "" = true isPaired expr = case stack of Nothing -> false (Just s) -> eq 0$ length s
where
stack =
foldl stackBracket Nothing
$map parseChar$ toCharArray expr

## 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?