diff options
| author | Jan Sucan <jan@jansucan.com> | 2023-09-03 11:57:33 +0200 |
|---|---|---|
| committer | Jan Sucan <jan@jansucan.com> | 2023-09-03 11:57:33 +0200 |
| commit | f5d1d49e3320e3f5407f77f232e4dcb1bb5ba380 (patch) | |
| tree | 37fb1b889a6430580b891532ddff560af264eb4d /ch08/8_c_1.hs | |
| parent | dc329e0c2801a843121cfefb9a701c96d2e5627b (diff) | |
8_c_2: Add solution
Diffstat (limited to 'ch08/8_c_1.hs')
| -rw-r--r-- | ch08/8_c_1.hs | 79 |
1 files changed, 0 insertions, 79 deletions
diff --git a/ch08/8_c_1.hs b/ch08/8_c_1.hs deleted file mode 100644 index ce80ade..0000000 --- a/ch08/8_c_1.hs +++ /dev/null @@ -1,79 +0,0 @@ --- Write a version of globToRegex that uses the type signature earlier. - -{-- From examples/examples/ch08/GlobRegex.hs modified according to the assignment --} -import Text.Regex.Posix ((=~)) - -type GlobError = String - -globToRegex :: String -> Either GlobError String - -globToRegex cs = propagateError regex - where regex = globToRegex' cs - propagateError (Left e) = Left e - propagateError (Right s) = Right ('^' : s ++ "$") - -globToRegex' :: String -> Either GlobError String -globToRegex' "" = Right "" - -globToRegex' ('*':cs) = propagateError regex - where regex = globToRegex' cs - propagateError (Left e) = Left e - propagateError (Right s) = Right (".*" ++ s) - -globToRegex' ('?':cs) = propagateError regex - where regex = globToRegex' cs - propagateError (Left e) = Left e - propagateError (Right s) = Right ("." ++ s) - -globToRegex' ('[':'!':c:cs) = propagateError charCls - where charCls = charClass cs - propagateError (Left e) = Left e - propagateError (Right s) = Right ("[^" ++ c : s) - -globToRegex' ('[':c:cs) = propagateError charCls - where charCls = charClass cs - propagateError (Left e) = Left e - propagateError (Right s) = Right ('[' : c : s) - -globToRegex' ('[':_) = Left "unterminated character class" - -globToRegex' (c:cs) = propagateError regex - where regex = globToRegex' cs - propagateError (Left e) = Left e - propagateError (Right s) = Right ((escape c) ++ s) - - -escape :: Char -> String -escape c | c `elem` regexChars = '\\' : [c] - | otherwise = [c] - where regexChars = "\\+()^$.{}]|" - - -charClass :: String -> Either GlobError String - -charClass (']':cs) = propagateError regex - where regex = globToRegex' cs - propagateError (Left e) = Left e - propagateError (Right s) = Right (']' : s) - -charClass (c:cs) = propagateError charCls - where charCls = charClass cs - propagateError (Left e) = Left e - propagateError (Right s) = Right (c : s) - -charClass [] = Left "unterminated character class" -{-- End of code from examples --} - - --- ghci> :l 8_c_1.hs --- [1 of 1] Compiling Main ( 8_c_1.hs, interpreted ) --- Ok, one module loaded. - --- ghci> globToRegex "[" --- Left "unterminated character class" - --- ghci> globToRegex "[]" --- Left "unterminated character class" - --- ghci> globToRegex "a?b*c[!DE][FG]+" --- Right "^a.b.*c[^DE][FG]\\+$" |
