File tree Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Original file line number Diff line number Diff line change
1
+ {-
2
+ Author: straykevin
3
+ Description: Recursively deletes the last occurance of the largest item in the list.
4
+ Language: Haskell
5
+
6
+ -}
7
+ rmax :: Ord a => [a ] -> [a ]
8
+ rmax [] = []
9
+ rmax xs =
10
+ let m = find_max xs
11
+ c = count m xs
12
+ in remove m c xs
13
+ where
14
+ find_max :: Ord t => [t ] -> t
15
+ find_max [x] = x
16
+ find_max (x : xs) =
17
+ let m = find_max xs
18
+ in if m > x then m else x
19
+
20
+
21
+ count :: Ord t => t -> [t ] -> Int
22
+ count a [] = 0
23
+ count a (x : xs) =
24
+ if x == a
25
+ then 1 + count a xs
26
+ else count a xs
27
+
28
+
29
+ remove :: Ord t => t -> Int -> [t ] -> [t ]
30
+ remove a c [] = []
31
+ remove a c (x : xs)
32
+ | x /= a = x : remove a c xs
33
+ | x == a && c == 1 = xs
34
+ | c < 1 = x : xs
35
+ | otherwise = x : remove a (c - 1 ) xs
You can’t perform that action at this time.
0 commit comments