module Exercise04 where import Data.Bifunctor import Data.Maybe {-WETT-} xmlLight :: String -> Bool xmlLight xml = helper xml [] helper :: String -> [String] -> Bool helper "" tags = null tags helper (f : xml) tags | f == '<' = if not (null xml) && head xml == '/' then not (null tags) && isJust endTag && head tags == fst (fromJust endTag) && snd (fromJust endTag) `helper` tail tags else isJust openTag && snd (fromJust openTag) `helper` (fst (fromJust openTag) : tags) -- | f == '<' && not (null xml) && head xml == '/' = not (null tags) && isJust endTag && head tags == fst (fromJust endTag) && snd (fromJust endTag) `helper` tail tags -- | f == '<' = isJust openTag && snd (fromJust openTag) `helper` (fst (fromJust openTag) : tags) | f == '>' = False | otherwise = helper xml tags where openTag = tagParser xml endTag = tagParser $ tail xml tagParser :: String -> Maybe (String,String) tagParser [] = Nothing tagParser (f : xml) | null xml = if f /= '>' then Nothing else Just ("", xml) | f == ' ' = case head xml of ' ' -> next '>' -> next _ -> Nothing | f == '<' = Nothing | f == '>' = Just ("", xml) | otherwise = if isJust next then Just $ first (f : ) (fromJust next) else Nothing where next = tagParser xml {-TTEW-}