myLast [x] = x myLast (x:xs) = myLast xs -- myButLast = myButLast' . reverse -- where -- myButLast' (x:y:xs) = y myButLast = head . tail. reverse elementAt (x:_) 1 = x elementAt (_:xs) n = elementAt xs (n-1) -- myLength xs = f xs 0 where -- f [] n = n -- f (_:xs) n = f xs (n+1) myLength [] = 0 myLength (x:xs) = 1 + myLength xs myLength1 :: [a]->Int myLength1 = foldr (\_->(+1)) 0 myLength2 = fst . last. zip [1..] myLength3 = sum . map (\_->1) myReverse :: [a]->[a] myReverse [] = [] myReverse (x:xs) = myReverse xs ++ [x] myReverse1 :: [a]->[a] myReverse1 = foldl (flip(:)) [] isPalindrome :: Eq a => [a] -> Bool isPalindrome xs = (xs == reverse xs) compress [] = [] compress (x:xs) = x:(compress $ rest x xs) where rest sample [] = [] rest sample (x:xs) | (sample /= x) = (x:xs) | otherwise = rest sample xs compress1 [] = [] compress1 (x:xs) = x:compress1 (dropWhile ((==) x) xs) pack [] = [] pack xs@(x:_) = (takeWhile cond xs): (pack $ dropWhile cond xs) where cond = (==) x encoode [] = [] encode xs = map (\ xs@(x:_)->(myLength xs, x)) (pack xs)