-
Notifications
You must be signed in to change notification settings - Fork 0
/
20150317.hs
128 lines (101 loc) · 2.5 KB
/
20150317.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
-- Quicksort
bigOfTwo :: Int -> Int -> Int
bigOfTwo a b
| a > b = a
| otherwise = b
quicksort :: [Int] -> [Int]
quicksort [] = []
quicksort (x:xs) =
let smallerSorted = quicksort [a | a <- xs, a <= x]
biggerSorted = quicksort [a | a <- xs, a > x]
in smallerSorted ++ [x] ++ biggerSorted
-- Sum digits
sumd 0 = 0
sumd x = (x `mod` 10) + sumd (x `div` 10)
ord :: [Int] -> [Int]
ord [] = []
ord [x] = [sumd x]
ord xs@(x:xt) = [sumd x] ++ ord(xt)
ordenar :: [Int] -> [Int]
ordenar xs = quicksort (ord xs)
fibonacci :: Int -> Int
fibonacci 0 = 1
fibonacci 1 = 1
fibonacci n = fibonacci (n - 1) + fibonacci (n - 2)
getEvens :: [Int] -> [Int]
getEvens [] = []
getEvens (a:x)
| mod a 2 == 0 = a:getEvens x
| otherwise = getEvens x
fiblist :: Int -> [Int]
fiblist a = [fibonacci a] ++ fiblist(a-1)
{-
Learning Material: not from class
-}
-- My not function
myNot :: Bool -> Bool
myNot True = False
myNot False = True
-- Increment integer by one
incr :: Int -> Int
incr e = (+) e 1
-- Concatenate two lists
sumLists :: [Int] -> [Int] -> [Int]
sumLists lista listb = (++) lista listb
-- Smaller of two integers
smallerOfTwo :: Int -> Int -> Int
smallerOfTwo a b
| a < b = a
| otherwise = b
-- Bigger of two integers
biggerOfTwo :: Int -> Int -> Int
biggerOfTwo a b
| a > b = a
| otherwise = b
-- Smaller of three integers
smallerOfThree :: Int -> Int -> Int -> Int
smallerOfThree a b c
| a < (smallerOfTwo b c) = a
| otherwise = (smallerOfTwo b c)
-- Bigger of three integers
biggerOfThree :: Int -> Int -> Int -> Int
biggerOfThree a b c
| a > (biggerOfTwo b c) = a
| otherwise = (biggerOfTwo b c)
factorial :: Int -> Int
factorial 0 = 1
factorial n = n * factorial (n-1)
-- Find out if an integer is within a list
contains :: Int -> [Int] -> Bool
contains _ [] = False
contains y (a:x)
| y == a = True
| otherwise = contains y x
count :: [t] -> Int
count [] = 0
count (a:x) = 1 + count x
search :: Int -> Int -> Bool
search a i
| i * i > a = True
| mod a i == 0 = False
| otherwise = search a (i+1)
isPrime :: Int -> Bool
isPrime a
| a <= 1 = False
| otherwise = search a 2
equals :: [Int] -> [Int] -> Bool
equals [] [] = True
equals _ [] = False
equals [] _ = False
equals (h:x) (g:y)
| h == g = equals x y
| otherwise = False
countOcurrences :: Int -> [Int] -> Int
countOcurrences _ [] = 0
countOcurrences y (a:x)
| y == a = 1 + countOcurrences y x
| otherwise = countOcurrences y x
bigger :: [Int] -> Int
bigger (a:x)
| count (a:x) == 1 = a
| otherwise = biggerOfTwo a (bigger x)