-
Notifications
You must be signed in to change notification settings - Fork 0
/
20150319.hs
74 lines (59 loc) · 1.82 KB
/
20150319.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
-- Defining the following list functions
-- double: Double the elements of a list
doubleElement :: Int -> Int
doubleElement n = (*) n 2
double :: [Int] -> [Int]
double list = map doubleElement list
-- membership: Tell if a element is in the list
member :: [Int] -> Int -> Bool
member list e
| (==) list [] = False
| otherwise = (==) (head list) e || member (tail list) e
-- this is cool, but if you can use a built-in function, you can do:
-- member :: [Int] -> Int -> Bool
-- member list e = e `elem` list
-- filtering: only the integers of a string
isDigit :: Char -> [Char]
isDigit ch
| ch >= '0' && ch <= '9' = ch : []
| otherwise = []
--
digits :: String -> String
digits str
| (==) str [] = []
| otherwise = isDigit (head str) ++ digits (tail str)
-- sum the elements of two lists, one-by-one
select :: [Int] -> [Int]
select list
| (==) list [] = [0]
| otherwise = list
sumPairs :: [Int] -> [Int] -> [Int]
sumPairs listx listy
| (==) listx [] && (==) listy [] = []
| otherwise = (head (select listx)) + (head (select listy)) : sumPairs (tail (select listx)) (tail (select listy))
takeAll :: (t -> Bool) -> [t] -> [t]
takeAll _ [] = []
takeAll pred (a:as)
| pred a = a : takeAll pred as
| otherwise = takeAll pred as
fib :: Int -> Int
fib 0 = 1
fib 1 = 1
fib n = fib (n - 1) + fib (n - 2)
getAll :: Int -> [Int]
getAll n
| length evens >= n = evens
| otherwise = getAll (n + 1)
where
as = fibList (n * 3) -- this is not mathematically correct, and is probably going to fail at some point
evens = getEvens as -- i'm just too lazy to think about it at 1 am
getEvens :: [Int] -> [Int]
getEvens [] = []
getEvens (a:x)
| mod a 2 == 0 = a:getEvens x
| otherwise = getEvens x
fibList :: Int -> [Int]
fibList 0 = []
fibList n = fib(n) : fibList(n - 1)
fibonacci :: Int -> [Int]
fibonacci n = reverse (getAll n)