module Exercise04 where import qualified Data.Text as T import qualified Data.List as L import Data.Char import Data.Maybe {-WETT-} xmlLight :: String -> Bool xmlLight s = isNothing (T.find checkBracket $L.head split) --edge case && all isJust tags_parsed --any invalid tags && stackValidation (catMaybes tags_parsed) [] where split = T.split (== '<') $T.pack s tags_parsed = map tagHelper $ tail split checkBracket :: Char -> Bool checkBracket c = c == '<' || c =='>' --Bool True if tag is opened, Nothing if invalid tag, strips the tag, tagHelper :: T.Text -> Maybe (Bool, T.Text) tagHelper tag = if valid then Just (opening, tag_stripped) else Nothing where tag_without_rest = (/= '>') `T.dropWhileEnd` tag tag_init = T.init tag_without_rest tag_stripped = if opening then T.stripEnd tag_init else T.tail $T.stripEnd tag_init valid = not (T.null tag_without_rest) && isNothing (T.find isSpace tag_stripped) opening = T.head tag /= '/' stackValidation :: [(Bool, T.Text)] -> [T.Text] -> Bool stackValidation [] stack = null stack stackValidation (t:tags) stack = if fst t --if opening then stackValidation tags $snd t:stack --recursive call with tag on stack else not (null stack) && head stack == snd t && stackValidation tags (tail stack) --validate stack, recursive call with tag removed from stack {-TTEW-}