Advertisement
Guest User

Untitled

a guest
Nov 28th, 2015
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.73 KB | None | 0 0
  1. last' :: [a] -> a
  2. last' [x] = x
  3. last' (_:xs) = last xs
  4.  
  5. butLast :: [a] -> a
  6. butLast [x,_] = x
  7. butLast (_:xs) = butLast xs
  8.  
  9. elementAt :: Int -> [a] -> a
  10. elementAt 0 xs = head xs
  11. elementAt n (x:xs) = elementAt (n-1) xs
  12.  
  13. length' :: [a] -> Int
  14. length' [] = 0
  15. length' (x:xs) = 1 + length' xs
  16.  
  17. reverse' :: [a] -> [a]
  18. reverse' [] = []
  19. reverse' (x:xs) = reverse' xs ++ [x]
  20.  
  21. isPalindrome :: Eq a => [a] -> Bool
  22. isPalindrome [] = True
  23. isPalindrome [_] = True
  24. isPalindrome xs
  25. | last xs == head xs = isPalindrome $ init $ tail xs
  26. | otherwise = False
  27.  
  28. -- No nested list structure in Haskell.
  29.  
  30. compress :: Eq a => [a] -> [a]
  31. compress [] = []
  32. compress [x] = [x]
  33. compress (x:xs)
  34. | x == head xs = compress xs
  35. | otherwise = x:compress xs
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement