Skip to content

Instantly share code, notes, and snippets.

@beoliver
Created May 31, 2016 15:49
Show Gist options
  • Save beoliver/2f434881c26ea8eb2bd9a456bfe6b29d to your computer and use it in GitHub Desktop.
Save beoliver/2f434881c26ea8eb2bd9a456bfe6b29d to your computer and use it in GitHub Desktop.
module Main where
import System.IO
import System.Environment (getArgs)
balancedSmileys ('(':xs) count = balancedSmileys xs (count + 1)
balancedSmileys (')':xs) count = (count > 0) && (balancedSmileys xs (count - 1))
balancedSmileys (':':x:xs) count = if (x == '(') || (x == ')')
then (balancedSmileys xs count) || (balancedSmileys (x:xs) count)
else (balancedSmileys (x:xs) count)
balancedSmileys (x:xs) count = balancedSmileys xs count
balancedSmileys [] count = count == 0
balancedSmileysWrapper xs =
case (balancedSmileys xs 0) of
True -> "YES"
False -> "NO"
main :: IO ()
main = do
args <- getArgs
handle <- openFile (head args) ReadMode
contents <- hGetContents handle
mapM_ (putStrLn . balancedSmileysWrapper) (lines contents)
hClose handle
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment