module Exercise04 where import Data.List (elemIndex) import Data.Maybe (isNothing, fromMaybe, fromJust) {-WETT-} xmlLight :: String -> Bool xmlLight = aux [] where aux tags string | '<' `notElem` string = notElem '>' string && null tags | isNothing mb || fromJust ((>) <$> startPosition <*> elemIndex '>' string) = False | closingTag = not(null tags) && head tags == tagId && aux (tail tags) (fromJust rest) | otherwise = aux (tagId:tags) $fromJust rest where startPosition = elemIndex '<' string tagLength = succ <$> (elemIndex '>' . flip drop string =<< startPosition) rest = flip drop string <$> ((+) <$> startPosition <*> tagLength) (tagId, closingTag) = fromJust mb mb = getTagId =<< flip take <$> (flip drop string <$> startPosition) <*> tagLength getTagId t = if all (==' ') $take (length t - end - 1)$drop end t then Just (drop (if closing then 2 else 1) $take end t, closing) else Nothing where end = fromMaybe (length t - 1) $elemIndex ' ' t closing = t !! 1 == '/' {-TTEW-}