-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex5.hs
More file actions
43 lines (26 loc) · 1.01 KB
/
ex5.hs
File metadata and controls
43 lines (26 loc) · 1.01 KB
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
-- file: Haskell/Graphic/ex5.hs
import Data.List (sortBy)
length :: [a] -> Int
length xs = foldl (\x _ -> x + 1) 0 xs
doubleEach = map (*2)
pairAndOne xs = zip xs $ map (+1) xs
addEachPair xs = [x+y | (x,y) <- xs]
maxList :: Ord a => [a] -> a
maxList = foldl1 max
minList :: Ord a => [a] -> a
minList = foldl1 min
addPointwise (a,b) (c,d) = (a+c, b+d)
addPairPointwise = foldl1 addPointwise
offset = fromEnum 'a'
maxcode = fromEnum 'z' - offset
encrypt xs = let orgmsg = map (flip (-) offset . fromEnum) xs
sndmsg = map (\x -> x+1 `mod` maxcode) orgmsg
in sndmsg
decrypt :: [Int] -> String
decrypt xs = let rsvmsg = map (\x -> if x-1 < 0 then maxcode else x-1) xs
orgmsg = map (toEnum . (+) offset) rsvmsg
in orgmsg
channel xs = xs == (decrypt . encrypt $ xs)
makeChange _ [] = []
makeChange amt (x:xs) = let (x',amt') = amt `divMod` x
in x' : makeChange amt' xs