-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhaskellFun4.hs
More file actions
84 lines (38 loc) · 1.41 KB
/
haskellFun4.hs
File metadata and controls
84 lines (38 loc) · 1.41 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import Data.Char
myProduct :: [Integer] -> Integer
myProduct [] = 1
myProduct (n:ns) = n * myProduct ns
shout :: String -> String
shout [] = ""
shout (c:cs) = toUpper c : shout cs
zap :: Char -> String -> String
zap c [] = ""
zap c (x:xs)
| c == x = zap c xs
| otherwise = x : zap c xs
pairUp :: [a] -> [(a,a)]
pairUp [] = []
pairUp (x:y:xs) = (x,y) : pairUp xs
pairUp (x:xs) = (x,x) : pairUp xs
neighbors :: [a] -> [(a,a)]
neighbors [] = []
neighbors [a] = []
neighbors (x:y:xs) = (x,y) : neighbors (y:xs)
bag1, bag2, bag3 :: [(Char, Int)]
bag1 = [('z',1), ('e',2), ('k',1)]
bag2 = [('y',2), ('a',1), ('n',1), ('c',1), ('e',1)]
bag3 = [('j',1), ('o',1), ('u',1), ('l',1), ('e',1)]
bagCount :: [(Char,Int)] -> Int
bagCount [] = 0
bagCount ((x,y):xs) = y + bagCount xs
addToBag :: Char -> [(Char,Int)] -> [(Char,Int)]
addToBag ch [] = [(ch,1)]
addToBag ch ((x,y):xs)
| ch == x = (x,y+1) : xs
| otherwise = (x,y) : addToBag ch xs
removeFromBag :: Char -> [(Char,Int)] -> [(Char,Int)]
removeFromBag ch [] = []
removeFromBag ch ((x,y):xs)
| ch == x && y == 1 = removeFromBag ch xs
| ch == x = (x,y-1) : xs
| otherwise = (x,y) : removeFromBag ch xs