-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
CommonWords.hs
32 lines (25 loc) · 850 Bytes
/
CommonWords.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 Main where
import Data.Set (fromList, toList, intersection)
import Data.Char (toLower)
fileToWords fileName = do
fileText <- readFile fileName
return $ (fromList . words) (map toLower fileText)
commonWords file1 file2 = do
words1 <- fileToWords file1
words2 <- fileToWords file2
return $ toList $ intersection words1 words2
commonWords2 file1 file2 =
fileToWords file1 >>= \f1 ->
fileToWords file2 >>= \f2 ->
return $ toList $ intersection f1 f2
commonWords3 file1 file2 =
(\f1 f2 -> toList $ intersection f1 f2)
<$> fileToWords file1
<*> fileToWords file2
main = do
cw <- commonWords "text1.txt" "text2.txt"
print cw
cw2 <- commonWords2 "text1.txt" "text2.txt"
print cw2
cw3 <- commonWords3 "text1.txt" "text2.txt"
print cw3