Skip to content

Commit 18ed473

Browse files
authored
Create rmax.hs
1 parent f16675b commit 18ed473

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

Haskell/rmax.hs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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

0 commit comments

Comments
 (0)