public
Created

  • Download Gist
lists.hs
Haskell
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
import Test.QuickCheck
import Test.QuickCheck.All
 
-- 01
last' :: [a] -> a
last' [x] = x
last' (_:xs) = last' xs
 
prop_last' xs = last' xs == last xs
 
-- 02
butLast :: [a] -> a
butLast [x,_] = x
butLast (_:xs) = butLast xs
 
-- 03
at :: Int -> [a] -> a
at 1 (x:_) = x
at n (_:xs) = at (n-1) xs
 
-- 04
length' :: [a] -> Int
length' [] = 0
length' [x] = 1
length' (_:xs) = 1 + length xs
 
length'' :: [a] -> Int
length'' xs = foldl (\acc _ -> acc + 1) 0 xs
 
length''' :: [a] -> Int
length''' xs = foldl (+) 0 map (\x -> 1) xs
 
-- 05
reverse' :: [a] -> [a]
reverse' [] = []
reverse' [x] = [x]
reverse' [x,y] = [y,x]
reverse' (x:xs) = (reverse' xs) ++ [x]
 
-- 06
palindrone :: (Eq a) => [a] -> Bool
palindrone xs = xs == reverse' xs
 
main = $(quickCheckAll)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.