-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdouble.hs
170 lines (132 loc) · 3.61 KB
/
double.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import Data.Char qualified
comparaLastNames name1 name2 =
if comp == EQ
then comparaFirstNames name1 name2
else comp
where
comp = compare lastName1 lastName2
lastName1 = snd name1
lastName2 = snd name2
comparaFirstNames name1 name2 =
compare firstName1 firstName2
where
firstName1 = fst name1
firstName2 = fst name2
sfOffice name =
if lastName > "L"
then fullName ++ " - PO Box 1234 - San Francisco, CA, 94111"
else fullName ++ " - PO Box 1010 - San Francisco, CA, 94109"
where
fullName = fst name ++ " " ++ snd name
lastName = snd name
nyOffice name = fullName ++ ": PO Box 789 - New York, NY, 10013"
where
fullName = fst name ++ " " ++ snd name
renoOffice name = lastName ++ " - PO Box 456 - Reno, NV 89523"
where
lastName = snd name
dcOffice name = fullName ++ " Esq"
where
fullName = fst name ++ " " ++ snd name
getLocation location = case location of
"ny" -> nyOffice
"sf" -> sfOffice
"reno" -> renoOffice
"dc" -> dcOffice
_ -> (\name -> fst name ++ " " ++ snd name)
addressLetter name location = locationFunc name
where
locationFunc = getLocation location
ifEven f x = if even x then f x else x
genIfEven f = (\x -> f x)
genIfXEven x = (\f -> f x)
getRequestUrl host apiKey resource id = host ++ "/" ++ resource ++ "/" ++ id ++ "?token=" ++ apiKey
getHostRequestBuilder host = (\apiKey resource id -> getRequestUrl host apiKey resource id)
firstHalfElem x xs =
x `elem` half || False
where
half = take ((length xs) `div` 2) xs
subSeq startind endind xs = drop startind $ take endind xs
gcd' a b =
if a `mod` b == 0
then b
else gcd' b $ a `mod` b
sayNumber :: Int -> String
sayNumber 1 = "one"
sayNumber 2 = "two"
sayNumber 3 = "three"
sayNumber _ = "other"
isEmpty' :: [a] -> Bool
isEmpty' [] = True
isEmpty' _ = False
myHead [] = error "that was an empty list you moron!"
myHead (x : _) = x
myTail [] = []
myTail (_ : xs) = xs
myGhd _ 1 = 1
myGhd x 0 = x
myGhd x y = myGhd y (x `mod` y)
myLength :: [a] -> Int
myLength [] = 0
myLength (_ : xs) = 1 + myLength xs
myTake :: Int -> [a] -> [a]
myTake _ [] = []
myTake 0 _ = []
myTake n (x : xs) = x : rest
where
rest = myTake (n - 1) xs
myDrop :: Int -> [a] -> [a]
myDrop _ [] = []
myDrop 0 xs = xs
myDrop 1 (_ : xs) = xs
myDrop n (_ : xs) = myDrop (n - 1) xs
ackermann 0 n = n + 1
ackermann m 0 = ackermann (m - 1) 1
ackermann m n = ackermann (m - 1) (ackermann m (n - 1))
collatz 1 = 1
collatz n =
if even n
then 1 + collatz (n `div` 2)
else 1 + collatz (n * 3 + 1)
reverse' :: [a] -> [a]
reverse' [] = []
reverse' (x : xs) = reverse' xs ++ [x]
fib 0 = 1
fib 1 = 1
fib n = fib (n - 1) + fib (n - 2)
myMap _ [] = []
myMap f (x : xs) = f x : myMap f xs
myFilter :: (a -> Bool) -> [a] -> [a]
myFilter _ [] = []
myFilter f (x : xs) =
if f x
then x : myFilter f xs
else myFilter f xs
remove' :: (a -> Bool) -> [a] -> [a]
remove' _ [] = []
remove' f (x : xs) =
if f x
then remove' f xs
else x : remove' f xs
myProduct [] = error "that is an empty list"
myProduct xs = foldl (*) 1 xs
concatAll [] = ""
concatAll xs = foldl (++) "" xs
myFoldl f init [] = init
myFoldl f init (x : xs) = myFoldl f newinit xs
where
newinit = f init x
myFoldr f init [] = init
myFoldr f init (x : xs) = f x rest
where
rest = myFoldr f init xs
myElem :: (Eq a) => a -> [a] -> Bool
myElem _ [] = False
myElem n xs = length newlist /= length xs
where
newlist = filter (/= n) xs
isPalindrome [] = True
isPalindrome xs = reducedString == reverse reducedString
where
reducedString = map Data.Char.toLower $ filter (/= ' ') xs
harmonicSum n = sum [1 / 1, 1 / 2 .. n]