-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstart.hs
83 lines (44 loc) · 1.6 KB
/
start.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
-- Functions
doubleMe x = x + x
doubleUs x y = x*2 + y*2
doubleSmallNumber x = if x > 100 then x else x * 2
myNameAsConstant = "Gabi Volpe"
-- Appending and prepending elems
myList = [1,2,3] ++ [4,5,6]
helloWorld = "Hello" ++ " " ++ "World"
helloWorld2 = "Hello " ++ ['W','o'] ++ "rld"
cat = 'A' : " small cat"
indice = "Gabriel Volpe" !! 8
-- Some lists functions
a = head [1,2,3,5]
b = tail [1,2,3,5]
c = last [1,2,3,5]
d = sum [1..99]
-- Infinite collections (Streams)
e = take 10 (cycle [1,2,3])
f = take 10 (repeat 5)
f2 = replicate 10 5 -- same result as f
-- More functions
g = [x*2 | x <- [1..10]]
-- Filtering functions
h = [x*2 | x <- [1..10], x*2 >= 12]
i = [ x | x <- [50..100], x `mod` 7 == 3]
boomBangs xs = [ if x < 10 then "BOOM!" else "BANG!" | x <- xs, odd x]
j = [ x | x <- [10..20], x /= 13, x /= 15, x /= 19]
k = [ x*y | x <- [2,5,10], y <- [8,10,11]]
l = [ x*y | x <- [2,5,10], y <- [8,10,11], x*y > 50]
nouns = ["Fender","Ibanez","Gibson"]
adjectives = ["Awesome", "Incredible"]
combination = [adj ++ " " ++ noun | adj <- adjectives, noun <- nouns]
length' xs = sum [1 | _ <- xs]
removeNonUppercase :: [Char] -> [Char] -- Explicit type definition
removeNonUppercase st = [ c | c <- st, c `elem` ['A'..'Z']]
-- Tuples
tuple = (28, "Gabi")
t1 = fst tuple
t2 = snd tuple
zipped = zip [1..5] "abcde"
-- Triangle exercise
triangles = [ (a,b,c) | c <- [1..10], b <- [1..10], a <- [1..10] ]
rightTriangles = [ (a,b,c) | c <- [1..10], b <- [1..c], a <- [1..b], a^2 + b^2 == c^2]
rightTriangles' = [ (a,b,c) | c <- [1..10], b <- [1..c], a <- [1..b], a^2 + b^2 == c^2, a+b+c == 24]