forked from Kaleb47/Haskell-Plutus-journal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lesson3.hs
73 lines (54 loc) · 2.13 KB
/
lesson3.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
import System.IO
myCompare :: (Ord a) => a -> a -> Ordering
a `myCompare` b
| a > b = GT
| a == b = EQ
| otherwise = LT
{-as you can see, the expression below is matched against the patterns-}
{-case expression of pattern -> result
pattern -> result
pattern -> result
... -}
describeList :: [a] -> String
describeList xs = "The list is " ++ what xs
where what [] = "empty."
what [x] = "a singleton list."
what xs = "a longer list."
{-this is replicate recursion. For example, you type in replicate' 5 5, the number 5 will be replicated 5 times-}
replicate' :: (Num i, Ord i) => i -> a -> [a]
replicate' n x
| n <= 0 = []
| otherwise = x:replicate' (n-1) x
{-take takes a specific set of variables from a list. The first pattern specifies that if you try to take a zero or negative number
the list will be empty. The third pattern breaks the list into a head and tail-}
take' :: (Num i, Ord i) => i -> [a] -> [a]
take' n _
| n <= 0 = []
take' _ [] = []
take' n (x:xs) = x : take' (n-1) xs
{-reverse reverse's a list-}
reverse' :: [a] -> [a] {-type signature-}
reverse' [] = []
reverse' (x:xs) = reverse' xs ++ [x] {-list is binded and clarifies that we want variables to be reverse-}
{-repeat takes an element and returns an infinite list of that element-}
repeat' :: a -> [a]
repeat' x = x:repeat' x
{-zip combines two lists by truncating the longer list-}
zip' :: [a] -> [b] -> [(a,b)]
zip' _ [] = []
zip' [] _ = []
zip' (x:xs) (y:ys) = (x,y):zip' xs ys
{-elem-}
elem' :: (Eq a) => a -> [a] -> Bool
elem' a [] = False
elem' a (x:xs)
| a == x = True
| otherwise = a `elem'` xs
{-this is quick sort. the algorithm will take the
head and put it in between lists that are bigger and smaller than it-}
quicksort :: (Ord a) => [a] -> [a]
quicksort [] = []
quicksort (x:xs) =
let smallerSorted = quicksort [a | a <- xs, a <= x]
biggerSorted = quicksort [a | a <- xs, a > x]
in smallerSorted ++ [x] ++ biggerSorted