-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWordCount.hs
77 lines (50 loc) · 1.69 KB
/
WordCount.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
module WordCount where
import Control.Arrow
main :: IO ()
main = do
contents <- readFile "1453275352867.txt"
let all = lines contents
mapM putStrLn $ getColumns (map (split ',') all) 0
return ()
-- the same as (list !! index)
takeColumn :: [String] -> Int -> String
takeColumn [] _ = ""
takeColumn (x:xs) n
| n == 0 = x
| n > 0 = takeColumn xs (n-1)
getColumns :: [[String]] -> Int -> [String]
getColumns [] _ = []
getColumns (x:xs) n = (x !! n) : (getColumns xs n)
split:: Char -> String -> [String]
split _ "" = []
split c xs = [h] ++ t
where h = takeWhile (/=c) xs
t = split c (tail $ dropWhile (/= c) xs)
count :: String -> Kleisli IO String ()
count w = Kleisli readFile >>>
arr words >>> arr (filter (/= w)) >>> arr (foldl (++) "") >>>
Kleisli putStrLn
tuple2Int :: ([a],[b]) -> (Int,[b])
tuple2Int a@(x,y) = tuple2F a length
tuple2F :: ([a],[b]) ->([a]->c) -> (c,[b])
tuple2F (x,y) f = (f x ,y)
--first :: (a->c) ->(a,b) -> (c,b)
--first f (x,y) = (f x ,y)
readInt :: String -> Int
readInt x = (read x)::Int
mydiv :: (Int,Int) -> Int
mydiv (x,y) = quot x y
myDiv :: (Int,Int) -> Float
myDiv (a,b) =
let x = fromIntegral a
y = fromIntegral b
in (x / y)
cross :: String -> Kleisli IO String ()
cross w = Kleisli readFile >>>
--arr words >>> arr (\x -> (x,x)) >>> arr tuple2Int >>>
--arr words >>> arr (\x -> (x,x)) >>> arr (length *** (map read)) >>> arr (second sum) >>> arr myDiv >>>
arr words
>>> arr (\x -> (x,x))
>>> arr (length *** (sum.(map read)))
>>> arr myDiv
>>> Kleisli print