-
Notifications
You must be signed in to change notification settings - Fork 0
/
StringSearching.hs
42 lines (35 loc) · 1.17 KB
/
StringSearching.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
module Main where
import System.Environment (getArgs)
import Data.List (tails)
data Entry = Literal Char | Star deriving (Show)
type Glob = [Entry]
construct :: String -> Glob
construct [] = []
construct ['*'] = [Star]
construct [x] = [Literal x]
construct (x:y:xs) = if x == '\\' && y == '*' then (Literal '*'):(construct xs) else
(if x == '*' then Star else Literal x):(construct (y:xs))
matchString :: Glob -> String -> Bool
matchString [] _ = True
matchString [Star] _ = True
matchString [_] [] = False
matchString (Literal _:_) [] = False
matchString (Literal g:gs) (x:xs) = x == g && matchString gs xs
matchString (Star:gs) str = or $ map (matchString gs) $ tails str
processLine :: String -> String
processLine l = case wordsWhen (== ',') l of
[str,pattern] -> showBool $ matchString (Star:(construct pattern)) str
_ -> "Invalid input"
wordsWhen :: (Char -> Bool) -> String -> [String]
wordsWhen p s = case dropWhile p s of
"" -> []
s' -> w : wordsWhen p s''
where (w, s'') = break p s'
showBool :: Bool -> String
showBool True = "true"
showBool _ = "false"
main :: IO ()
main = do
[inpFile] <- getArgs
input <- readFile inpFile
mapM_ putStrLn $ map processLine $ lines input