-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLearning.hs
More file actions
109 lines (85 loc) · 2.65 KB
/
Copy pathLearning.hs
File metadata and controls
109 lines (85 loc) · 2.65 KB
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
import Data.List
-- Sieve of Eratosthenes
sieve :: Integral a => a -> [a]
sieve n = [ x| x <-[2..n], all (\f-> x `mod` f /= 0) [2..floor(sqrt(fromIntegral x))] ]
-- A filter funciton
filter' :: (a->Bool) -> [a] -> [a]
filter' p [] = []
filter' p (x:xs)
| p x = x : filter' p xs
| otherwise = filter' p xs
count l x = foldl (\acc elmet -> if elmet == x then acc+1 else acc) 0 l
sum1 :: Num a => [a] -> a
sum1 [] = 0
sum1 (x:xs) = x + sum1(xs)
-- Problem # 1
myLast :: [a] -> a
myLast [x] = x
myLast (x:xs) = myLast xs
-- Problem # 2
myButLast :: [t] -> t
myButLast l = last (init l)
-- Problem # 3
elementAt :: [t] -> Int -> t
elementAt (x:xs) i = if i == 1 then x else elementAt xs (i-1)
-- Problem # 4
myLength :: [t] -> Int
myLength l = foldl (\acc elmet -> acc +1) 0 l
-- Problem # 5
myReverse :: [t] -> [t]
myReverse l = foldl (\acc elmet -> elmet : acc) [] l
-- Problem # 6
isPalindrome :: (Eq t) => [t] -> Bool
isPalindrome l = if myReverse l == l then True else False
-- Problem # 7
data NestedList a = Elem a | List [NestedList a]
flatten :: NestedList a -> [a]
flatten (Elem x) = [x]
flatten (List x) = concatMap flatten x
--flatten (Elem x) = [x]
--flatten (List (x:xs)) = flatten x ++ flatten (List xs)
--flatten (List []) = []
-- Problem # 8
compress :: (Eq a) => [a] -> [a]
compress [] = []
compress [x] = [x]
compress (x:xs) = if x == head (compress xs) then compress xs else x:compress xs
-- Problem # 9
pack :: (Eq a) => [a] -> [[a]]
pack [] = []
pack [x] = [[x]]
pack (x:xs) = if x `elem` (head (pack xs))
then (x:(head (pack xs))):(tail (pack xs))
else [x]:(pack xs)
-- Problem # 10
encode :: (Eq a) => [a] -> [(Int,a)]
encode x = map (\l -> (length l, head l)) (group x)
-- Problem # 11
data ListElemt a = Single a | Multiple Int a
deriving (Show)
encodeModified :: (Eq a) => [a] -> [ListElemt a]
encodeModified xs = map helper (encode xs)
where helper (1,a) = Single a
helper (n,a) = Multiple n a
-- Problem # 12
-- Problem # 14
dupli :: [a] -> [a]
dupli l = foldl (\acc elm -> acc ++ [elm,elm]) [] l
-- Problem # 15
relpi :: [a] -> Int -> [a]
relpi l n = concatMap (replicate n) l
-- Problem # 16
dropEvery :: [a] -> Int -> [a]
-- Assuming 0 is first index
dropEvery x n = fst $ foldl helper ([],0) x
where helper (acc,index) elemt = if index == n then (acc,0) else (acc++[elemt],index+1)
-- Problem # 17
split :: [a] -> Int -> ([a],[a])
split l x = fst $ foldl helper (([],[]),0) l
where helper ((left,right),i) elm = if i < x then ((left++[elm],right),i+1) else ((left,right++[elm]),i+1)
-- Problem #18
slice :: [a] -> Int -> Int -> [a]
slice [] x y = []
-- Problem #19
rotate :: [a] -> Int -> [a]
rotate [a] _ = [a]