-----------------------------------------------------------------------------
-- Copyright 2019, Advise-Me project team. This file is distributed under 
-- the terms of the Apache License 2.0. For more information, see the files
-- "LICENSE.txt" and "NOTICE.txt", which are included in the distribution.
-----------------------------------------------------------------------------
-- |
-- Maintainer  :  bastiaan.heeren@ou.nl
-- Stability   :  provisional
-- Portability :  portable (depends on ghc)
--
-----------------------------------------------------------------------------

module Util.Parentheses where

balanced :: String -> Bool
balanced = rec ""
 where
  rec st [] = null st
  rec st (x:xs)
      | x `elem` "([{" = rec (x:st) xs
      | x `elem` ")]}" = match (take 1 st) x && rec (drop 1 st) xs
      | otherwise      = rec st xs

  match :: String -> Char -> Bool
  match "(" ')' = True
  match "[" ']' = True
  match "{" '}' = True
  match _   _   = False