Created
November 28, 2015 11:14
-
-
Save nbrick/1345a14641f0700932f3 to your computer and use it in GitHub Desktop.
Ninety-Nine Haskell Problems: Solutions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
last' :: [a] -> a | |
last' [x] = x | |
last' (_:xs) = last xs | |
butLast :: [a] -> a | |
butLast [x,_] = x | |
butLast (_:xs) = butLast xs | |
elementAt :: Int -> [a] -> a | |
elementAt 0 xs = head xs | |
elementAt n (x:xs) = elementAt (n-1) xs | |
length' :: [a] -> Int | |
length' [] = 0 | |
length' (x:xs) = 1 + length' xs | |
reverse' :: [a] -> [a] | |
reverse' [] = [] | |
reverse' (x:xs) = reverse' xs ++ [x] | |
isPalindrome :: Eq a => [a] -> Bool | |
isPalindrome [] = True | |
isPalindrome [_] = True | |
isPalindrome xs | |
| last xs == head xs = isPalindrome $ init $ tail xs | |
| otherwise = False | |
-- No nested list structure in Haskell. | |
compress :: Eq a => [a] -> [a] | |
compress [] = [] | |
compress [x] = [x] | |
compress (x:xs) | |
| x == head xs = compress xs | |
| otherwise = x:compress xs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment