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

Published at Jul 28 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

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

```
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.Foldable (foldl)
import Data.List (List(..), (:))
import Data.Maybe (Maybe(..))
import Data.String (toCharArray)
import Data.Tuple (Tuple(..))
data Stack a = Stack (List a)
push :: forall a. Stack a -> a -> Stack a
push (Stack l) el = Stack (el : l)
pop :: forall a. Stack a -> Tuple (Maybe a) (Stack a)
pop (Stack Nil) = Tuple Nothing (Stack Nil)
pop (Stack (x : xs)) = Tuple (Just x) (Stack xs)
type Bracket =
{ btype :: BType
, direction :: Direction
}
data BType = Curly | Square | Parens
derive instance eqBType :: Eq BType
data Direction = Open | Closed
charToBracket :: Char -> Maybe Bracket
charToBracket '{' = Just { btype: Curly, direction: Open }
charToBracket '[' = Just { btype: Square, direction: Open }
charToBracket '(' = Just { btype: Parens, direction: Open }
charToBracket '}' = Just { btype: Curly, direction: Closed }
charToBracket ']' = Just { btype: Square, direction: Closed }
charToBracket ')' = Just { btype: Parens, direction: Closed }
charToBracket _ = Nothing
data Balance
= OpenImbalanced (Stack Bracket)
| ClosedImbalanced
isPaired :: String -> Boolean
isPaired s =
case checkBalance of
OpenImbalanced (Stack Nil) -> true
_ -> false
where
checkBalance = foldl updateBalance (OpenImbalanced (Stack Nil)) (toCharArray s)
updateBalance :: Balance -> Char -> Balance
updateBalance ClosedImbalanced _ = ClosedImbalanced
updateBalance (OpenImbalanced brackets) c =
case charToBracket c, pop brackets of
Just { direction: Closed }, (Tuple Nothing _) -> ClosedImbalanced
Just { direction: Closed, btype }, (Tuple (Just topBracket) rest)
| btype == topBracket.btype -> OpenImbalanced rest
| otherwise -> ClosedImbalanced
Just b@{ direction: Open, btype }, _ -> OpenImbalanced $ push brackets b
Nothing, _ -> OpenImbalanced brackets
```

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?

Level up your programming skills with 3,450 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments