-- CA215 Languages and Computability 2008-2009 -- Revision import Char -- Takes a list of Int and returns a list containing only the even ones evens :: [Int] -> [Int] evens []= [] evens (x:xs) | x `mod` 2 == 0 = x : evens xs | otherwise = evens xs evens2 :: [Int] -> [Int] evens2 xs = filter isEven xs where isEven x = x `mod` 2 == 0 -- Takes a list of Char and returns a list containing only the consonants (assumes lowercase letters) consonants :: [Char] -> [Char] consonants [] = [] consonants (x:xs) | x == 'a' || x == 'e' || x == 'i' || x == 'o' || x == 'u' = consonants xs | otherwise = x : consonants xs -- Takes a list and returns the reverse myReverse :: [a] -> [a] myReverse [] = [] myReverse (x:xs) = myReverse xs ++ [x] -- Takes a list of Char and returns the number of vowels vowels :: [Char] -> Int vowels [] = 0 vowels (x:xs) | x == 'a' || x == 'e' || x == 'i' || x == 'o' || x == 'u' = 1 + vowels xs | otherwise = vowels xs -- stringToUpper in terms of list comprehension stringToUpper :: String -> String stringToUpper xs = [ toUpper x | x <- xs] -- Takes a list of characters as input and outputs a list of corresponding ASCII codes (Int) convertChar :: String -> [Int] convertChar xs = [ ord x | x <- xs] convertChar' :: String -> [Int] convertChar' [] = [] convertChar' (x:xs) = ord x : convertChar' xs -- Takes two lists and appends the second to the end of the first appendLists :: [a] -> [a] -> [a] appendLists [] [] = [] appendLists xs [] = xs appendLists [] ys = ys appendLists xs ys = xs ++ ys appendLists2 :: [a] -> [a] -> [a] appendLists2 [] [] = [] appendLists2 xs [] = xs appendLists2 [] ys = ys appendLists2 (x:xs) ys = x : appendLists2 xs ys -- Takes two lists and merges them into a single list by putting the first element of list1 then the first element of list2, then the second element of list1 etc. mergeLists :: [a] -> [a] -> [a] mergeLists [] [] = [] mergeLists xs [] = xs mergeLists [] ys = [] mergeLists (x:xs) (y:ys) = x : y : mergeLists xs ys -- Takes two lists of Ints and returns a list of products by multiplying the first element of list1 with the first element of list2 etc. multiplyLists :: [Int] -> [Int] -> [Int] multiplyLists [] [] = [] multiplyLists xs [] = xs multiplyLists [] ys = [] multiplyLists (x:xs) (y:ys) = x*y : multiplyLists xs ys -- Higher order sort hosort :: (a -> a -> Bool) -> [a] -> [a] hosort p [] = [] hosort p (x:xs) = hosort p [y | y <- xs, p y x] ++ [x] ++ hosort p [y | y <- xs, not (p y x)]