-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDoubleWords.hs
32 lines (24 loc) · 974 Bytes
/
DoubleWords.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
module DoubleWords(main) where
import Control.Monad
import Data.Char
import Data.Function
import Data.List
import System.Environment
main = do
files <- getArgs
when (null files) $ error "Enter list of files to check"
putStrLn "Checking for duplicates"
bad <- fmap concat $ forM files $ \file -> do
src <- readFile file
return $ map ((,) file) $ dupes $ worder src
putStr $ unlines $ if null bad then ["Success"] else
[file ++ ":" ++ show line ++ ": " ++ word | (file,(line,word)) <- bad] ++
["FAILURES (" ++ show (length bad) ++ ")"]
worder :: String -> [(Int, String)]
worder str = [(i,s)
| (i, s) <- zip [1..] $ lines str
, s <- words $ concatMap (\x -> if isAlpha x then [toLower x] else if isSpace x then " " else " 1 2 ") s
, s `notElem` ["l","c","v"]
]
dupes :: Eq v => [(k, v)] -> [(k, v)]
dupes = map head . filter ((> 1) . length) . groupBy ((==) `on` snd)