Vous êtes sur la page 1sur 26

Lists and list patterns

Lists and list patterns

1 Created By Mr. Deependra Rastogi, Lecturer , Department of Computer Science,TMU

Lists and list patterns

2 Created By Mr. Deependra Rastogi, Lecturer , Department of Computer Science,TMU

Lists and list patterns

3 Created By Mr. Deependra Rastogi, Lecturer , Department of Computer Science,TMU

Lists and list patterns

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

Primitive recursion over lists

4 Created By Mr. Deependra Rastogi, Lecturer , Department of Computer Science,TMU

Lists and list patterns

Question 3

5 Created By Mr. Deependra Rastogi, Lecturer , Department of Computer Science,TMU

Lists and list patterns

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

Lists and list patterns

Finding primitive recursive definitions

7 Created By Mr. Deependra Rastogi, Lecturer , Department of Computer Science,TMU

Lists and list patterns

8 Created By Mr. Deependra Rastogi, Lecturer , Department of Computer Science,TMU

Lists and list patterns

9 Created By Mr. Deependra Rastogi, Lecturer , Department of Computer Science,TMU

Lists and list patterns

10 Created By Mr. Deependra Rastogi, Lecturer , Department of Computer Science,TMU

Lists and list patterns

11 Created By Mr. Deependra Rastogi, Lecturer , Department of Computer Science,TMU

Lists and list patterns

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

Lists and list patterns


deleteAll :: Eq a => a -> [a] -> [a] deleteAll x [] = [] deleteAll x (y:ys) | x == y = (deleteAll x ys) | otherwise = (y : ( deleteAll x ys ) )

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

13 Created By Mr. Deependra Rastogi, Lecturer , Department of Computer Science,TMU

Lists and list patterns

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:

14 Created By Mr. Deependra Rastogi, Lecturer , Department of Computer Science,TMU

Lists and list patterns

15 Created By Mr. Deependra Rastogi, Lecturer , Department of Computer Science,TMU

Lists and list patterns

Question 9
16 Created By Mr. Deependra Rastogi, Lecturer , Department of Computer Science,TMU

Lists and list patterns

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

Lists and list patterns

Solution

import Prelude hiding(zip3)

--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

Lists and list patterns


sublist (x:xs) (y:ys) | x == y && sublist xs ys = True | sublist (x:xs) ys = True | otherwise = False subs :: Eq a => [a] -> [a] -> Bool subs [] _ = True subs _ [] = False subs (x:xs) (y:ys) | x == y && xs == [] = True | ys == [] && xs /= [] = False | x == y && x1 == y1 && subs x1s y1s = True | subs (x:xs) ys = True | otherwise = False where (x1:x1s) = xs (y1:y1s) = ys

19 Created By Mr. Deependra Rastogi, Lecturer , Department of Computer Science,TMU

Lists and list patterns

20 Created By Mr. Deependra Rastogi, Lecturer , Department of Computer Science,TMU

Lists and list patterns

21 Created By Mr. Deependra Rastogi, Lecturer , Department of Computer Science,TMU

Lists and list patterns

22 Created By Mr. Deependra Rastogi, Lecturer , Department of Computer Science,TMU

Lists and list patterns

23 Created By Mr. Deependra Rastogi, Lecturer , Department of Computer Science,TMU

Lists and list patterns

24 Created By Mr. Deependra Rastogi, Lecturer , Department of Computer Science,TMU

Lists and list patterns

Question

25 Created By Mr. Deependra Rastogi, Lecturer , Department of Computer Science,TMU

Lists and list patterns

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

removeNewLines :: String -> String removeNewLines s = [ x | x <- s, x /= '\n']

26 Created By Mr. Deependra Rastogi, Lecturer , Department of Computer Science,TMU

Vous aimerez peut-être aussi