Skip to content

@darkhelmet /lists.hs
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.