-
Notifications
You must be signed in to change notification settings - Fork 0
/
01.hs
45 lines (34 loc) · 1023 Bytes
/
01.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
module Haskell9901To10 where
import Data.List
import Control.Arrow
myLast :: [a] -> a
myLast [] = error "You cannot get last of an empty list you dummy"
myLast [x] = x
myLast (_:xs) = myLast xs
myButLast :: [a] -> a
myButLast [] = error "This list empty"
myButLast [_] = error "This list has one element"
myButLast xs =
let len = length xs
in xs !! (len - 2)
elementAt :: Int -> [a] -> a
elementAt n xs = xs !! n
myLength :: [a] -> Int
myLength [] = 0
myLength (_:xs) = 1 + myLength xs
reverse' :: [a] -> [a]
reverse' [] = []
reverse' (x:xs) = reverse' xs ++ [x]
isPalindrome :: String -> Bool
isPalindrome xs = xs == reverse xs
compress :: Eq a => [a] -> [a]
compress [] = []
compress (x:xs) = if x `notElem` xs then x:compress xs else compress xs
compress' :: Eq a => [a] -> [a]
compress' = map head . group
pack :: Eq a => [a] -> [[a]]
pack [] = []
pack (x:xs) = let (hd, tl) = span (== x) xs
in (x:hd) : pack tl
encode :: Eq a => [a] -> [(Int, a)]
encode = map (length &&& head) . group