Lists and List Patterns
-
Upload
sankulsybca -
Category
Documents
-
view
226 -
download
0
Transcript of Lists and List Patterns
-
7/27/2019 Lists and List Patterns
1/26
Lists and list patterns
Lists and list patterns
1 Created ByMr. Deependra Rastogi, Lecturer , Department of ComputerScience,TMU
-
7/27/2019 Lists and List Patterns
2/26
Lists and list patterns
2 Created ByMr. Deependra Rastogi, Lecturer , Department of ComputerScience,TMU
-
7/27/2019 Lists and List Patterns
3/26
Lists and list patterns
3 Created ByMr. Deependra Rastogi, Lecturer , Department of ComputerScience,TMU
-
7/27/2019 Lists and List Patterns
4/26
Lists and list patterns
Question 1
Solution
firstDigitPlusOne :: [Int] -> IntfirstDigitPlusOne x
= case ( x ) of[] -> 0xs -> (head xs + 1)
Question 2
Solution
addTwo :: [Int] -> IntaddTwo [] = 0addTwo (x:[]) = x
addTwo (x:y:[]) = x + y
Primitive recursion over lists
4 Created ByMr. Deependra Rastogi, Lecturer , Department of ComputerScience,TMU
-
7/27/2019 Lists and List Patterns
5/26
Lists and list patterns
Question 3
5 Created ByMr. Deependra Rastogi, Lecturer , Department of ComputerScience,TMU
-
7/27/2019 Lists and List Patterns
6/26
Lists and list patterns
Solution
import Prelude hiding(product)
product :: [Int] -> Int--product [] = 1 is needed to give simple recursive definitionproduct [] = 1product (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 [] = Trueand (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 [] = Falseor (x:xs)
| x == True = True| otherwise = or xs
6 Created ByMr. Deependra Rastogi, Lecturer , Department of ComputerScience,TMU
-
7/27/2019 Lists and List Patterns
7/26
Lists and list patterns
Finding primitive recursive definitions
7 Created ByMr. Deependra Rastogi, Lecturer , Department of ComputerScience,TMU
-
7/27/2019 Lists and List Patterns
8/26
Lists and list patterns
8 Created ByMr. Deependra Rastogi, Lecturer , Department of ComputerScience,TMU
-
7/27/2019 Lists and List Patterns
9/26
Lists and list patterns
9 Created ByMr. Deependra Rastogi, Lecturer , Department of ComputerScience,TMU
-
7/27/2019 Lists and List Patterns
10/26
Lists and list patterns
10 Created ByMr. Deependra Rastogi, Lecturer , Department of ComputerScience,TMU
-
7/27/2019 Lists and List Patterns
11/26
Lists and list patterns
11 Created ByMr. Deependra Rastogi, Lecturer , Department of ComputerScience,TMU
-
7/27/2019 Lists and List Patterns
12/26
Lists and list patterns
Question 6
Solution
unique :: [Int] -> [Int]unique [] = []unique (x:xs)
| (notElem x xs) = (x: (unique xs))| otherwise = unique ( deleteAll x xs)
--deleteAll x xs: returns list xs with all elements thatequaled x deleted
12 Created ByMr. Deependra Rastogi, Lecturer , Department of ComputerScience,TMU
-
7/27/2019 Lists and List Patterns
13/26
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 helpsfsts :: [(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 ByMr. Deependra Rastogi, Lecturer , Department of ComputerScience,TMU
-
7/27/2019 Lists and List Patterns
14/26
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 thefunction 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 areavailable for finding function definitions over lists. In trying 10 userecursion over lists to define a function we need to pose the question:
14 Created ByMr. Deependra Rastogi, Lecturer , Department of ComputerScience,TMU
-
7/27/2019 Lists and List Patterns
15/26
Lists and list patterns
15 Created ByMr. Deependra Rastogi, Lecturer , Department of ComputerScience,TMU
-
7/27/2019 Lists and List Patterns
16/26
Lists and list patterns
Question 9
16 Created ByMr. Deependra Rastogi, Lecturer , Department of ComputerScience,TMU
-
7/27/2019 Lists and List Patterns
17/26
Lists and list patterns
Solution
import Prelude hiding(drop,splitAt)
drop :: Int -> [a] -> [a]splitAt :: Int -> [a] -> ([a],[a])
drop 0 x = xdrop 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
-
7/27/2019 Lists and List Patterns
18/26
Lists and list patterns
Solutionimport 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 x ] ++ [x] ++ qSort [ y | y [a] -> [a] -> Bool
sublist [] _ = True
sublist _ [] = False18 Created By
Mr. Deependra Rastogi, Lecturer , Department of ComputerScience,TMU
-
7/27/2019 Lists and List Patterns
19/26
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 [] _ = Truesubs _ [] = Falsesubs (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 ByMr. Deependra Rastogi, Lecturer , Department of ComputerScience,TMU
-
7/27/2019 Lists and List Patterns
20/26
Lists and list patterns
20 Created ByMr. Deependra Rastogi, Lecturer , Department of ComputerScience,TMU
-
7/27/2019 Lists and List Patterns
21/26
Lists and list patterns
21 Created ByMr. Deependra Rastogi, Lecturer , Department of ComputerScience,TMU
-
7/27/2019 Lists and List Patterns
22/26
Lists and list patterns
22 Created ByMr. Deependra Rastogi, Lecturer , Department of ComputerScience,TMU
-
7/27/2019 Lists and List Patterns
23/26
Lists and list patterns
23 Created ByMr. Deependra Rastogi, Lecturer , Department of ComputerScience,TMU
-
7/27/2019 Lists and List Patterns
24/26
Lists and list patterns
24 Created ByMr. Deependra Rastogi, Lecturer , Department of ComputerScience,TMU
-
7/27/2019 Lists and List Patterns
25/26
Lists and list patterns
Question
25 Created ByMr. Deependra Rastogi, Lecturer , Department of ComputerScience,TMU
-
7/27/2019 Lists and List Patterns
26/26
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