Skip to content

Instantly share code, notes, and snippets.

@nbrick
Created November 28, 2015 11:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nbrick/1345a14641f0700932f3 to your computer and use it in GitHub Desktop.
Save nbrick/1345a14641f0700932f3 to your computer and use it in GitHub Desktop.
Ninety-Nine Haskell Problems: Solutions
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