Académique Documents
Professionnel Documents
Culture Documents
Question 1
Solution firstDigitPlusOne :: [Int] -> Int firstDigitPlusOne x = case ( x ) of [] -> 0 xs -> (head xs + 1) Question 2
Solution addTwo addTwo addTwo addTwo :: [Int] -> Int [] = 0 (x:[]) = x (x:y:[]) = x + y
Question 3
Solution import Prelude hiding(product) product :: [Int] -> Int --product [] = 1 is needed to give simple recursive definition product [] = 1 product (x:xs) = x * product xs
Question 4
Solution import Prelude hiding(and,or) and, or :: [Bool] -> Bool -- and [] = True: x and y and z and True == x and y and z -- allows simple recursion and [] = True and (x:xs) | x == False = False | otherwise = and xs -- or [] = False: x or y or z or False = x or y or z -- allows simple recursion or [] = False or (x:xs) | x == True = True | otherwise = or xs
6 Created By Mr. Deependra Rastogi, Lecturer , Department of Computer Science,TMU
Question 6
Solution
unique unique unique | | :: [Int] -> [Int] [] = [] (x:xs) (notElem x xs) = (x: (unique xs)) otherwise = unique ( deleteAll x xs)
--deleteAll x xs: returns list xs with all elements that equaled x deleted
12 Created By Mr. Deependra Rastogi, Lecturer , Department of Computer Science,TMU
Question 7
Solution import Prelude hiding(reverse,unzip) reverse :: [a] -> [a] reverse [x] = [x] reverse x = ( (last x) : (reverse (init x)) ) unzip :: [(a,b)] -> ([a],[b]) unzip [(x,y)] = ([x],[y]) unzip ( (x,y) : xys ) = ( (x: --???????????????????????? --this helps fsts :: [(a,b)] -> [a] snds :: [(a,b)] -> [b] fsts [] = [] fsts ( (x,y) : xs ) = (x : (fsts xs)) snds [] = [] snds ( (x,y) : ys ) = ( y: (snds ys)) Question 8
Solution
import List ins :: Int -> [Int] -> [Int] ins x [] = [x] ins x (y:ys) | x >= y = x:(y:ys) | otherwise = y:( ins x ys ) ins2 :: Int -> [Int] -> [Int] ins2 x [] = [x] ins2 x (y:ys) | x < y = nub (x:(y:ys)) | x == y = nub (y:ys) | otherwise = nub (y : ins2 x ys) General recursions over lists
A recursive definition of a function need not always use the value of the function on the tail; any recursive call to a value on a . Simpler list will be legitimate, and so a number of different patterns of recursion are available for finding function definitions over lists. In trying 10 use recursion over lists to define a function we need to pose the question:
Question 9
16 Created By Mr. Deependra Rastogi, Lecturer , Department of Computer Science,TMU
Solution import Prelude hiding(drop,splitAt) drop :: Int -> [a] -> [a] splitAt :: Int -> [a] -> ([a],[a]) drop 0 x = x drop n [] = [] drop n (x:xs) = drop (n-1) xs splitAt 0 x = ([],x) splitAt n [] = ([],[]) splitAt n xs = (take n xs, drop n xs)
Question 10
Solution --The result would be []. import Prelude hiding(take) take 0 _ = [] take n (x:xs) | n>0 = x : take (n-1) xs take x y | x<0 = error "PreludeListe.take: negative argument" take _ [] = [] Question 11
17 Created By Mr. Deependra Rastogi, Lecturer , Department of Computer Science,TMU
Solution
--recursively: zip3_1 :: [a]->[b]->[c] -> [(a,b,c)] zip3_1 (x:[]) (y:[]) (z:[]) = [(x,y,z)] zip3_1 (x:xs) (y:ys) (z:zs) = (x,y,z):(zip3_1 xs ys zs) --using zip: --zip3_2 :: [a]->[b]->[c] -> [(a,b,c)] --zip3_2 (x:xs) (y:ys) (z:zs) = zip --mmmh... don't know how to do that
Question 12 Solution
qSort :: [Int] -> [Int] qSort [] = [] qSort (x:xs) = qSort [ y | y<-xs, y > x ] ++ [x] ++ qSort [ y | y<-xs, y < x ]
Question 13
Solution
sublist :: Eq a => [a] -> [a] -> Bool sublist [] _ = True sublist _ [] = False
18 Created By Mr. Deependra Rastogi, Lecturer , Department of Computer Science,TMU
Question
Solution wc :: String -> (Int, Int, Int) wc "" = (0, 0, 0) wc ss = ( c + c', w + w', 1 + l') where c = length (removeNewLines s) w = length (splitWords (removeNewLines s)) (c', w', l') = wc s' s = firstLine ss s' = drop (length s) ss
firstLine :: String -> String firstLine "" = "" firstLine ('\n':ss) = ['\n'] firstLine (s:ss) = s:firstLine ss