صفحه 1:
Lists Lecture 8 Lists

صفحه 2:
A list is a very useful structure. A list is a collection of items of the same type. ‎a list of characters‏ 01 6ط ة] ‎[fic2,4b},"hhh"] not an acceptable list‏ ‎Main> [1, 2, ‘h', "hhh"]‏ ‎ERROR - Type error in list + Expression — : [1,'h',"hhh"] ‎+e Term: : "hhh" ee Type : String *** Does not match : Char [109,26,5,8,19] a list of Ints [Int] [(a’,97),(‘r’,114),(‘m’,109)] a list of pairs [(Char,Int)] ‎["Mon'","Tue","Wed","Thur","Fri","Sat","Sun"] a list of Strings {Sieiegs a special list called the empty list or a list with‘ abcélé. 5 comperters ‏حا‎ a list of String3 ‏1ب کنو ۲6

صفحه 3:
There is a special notation used with lists which is written as [n .. m]. This is read as: nup tom. Example: [2..9] which yields [2, 3, 4, 5, 6, 7, 8, 9] Using this notation, some useful sequences can be constructed: [1..99] [1,5..10] [173512] [i533] [a7bJ ['a’,'c!..] Lecture 8 Lists

صفحه 4:
Some Examples Prelude> take 10 [1,3..] [1,3,5,7,9,11,13,15,17,19] Prelude> take 10 ['a','b'..] "abcdefghij" Prelude> take 10 ['a','c'..] "acegikmogs" Prelude> take 10 [0,2..] [0,2,4,6,8,10,12,14,16,18] Prelude> [1,5..10] [1,5,9] Prelude> [1,3..12] [1,3,5,7,9,11] Lecture 8 Lists

صفحه 5:
A list can either be the empty list ۱1 or it may consist of a -head (the first element) and a tail (the Tes) ead tail) “read as head cons tail” — [2,4,8,19,55,99,1,981] head = 2 tail= [4,8,19,55,99,1,981] There are two built-in functions that return the head and tail of a list Prelude> head [1,2,3,4] 1 Prelude> tail [1,2,3,4] [2,3,4] Lecture 8 Lists 5

صفحه 6:
ist [11, 21, 31,14] in bracket notation, can be written as: 11: 21 : 1 14] (known as the cons notation) | xample: addList :: [Int] -> Int addList [] = 0 addList (x:xs) = x + addList xs The brackets [ ] and the "cons" operator": " are examples of constructors. These are the the constructors for lists. Lecture 8 Lists 6

صفحه 7:
1 then else if boolean-expression then. expression-1 else expression-2 if a>b then a else b maxiab=if a>b then a else b Main> maxi 2 3 3 Main> maxi 4 2 4 Lecture 8 Lists

صفحه 8:
tion to remove an element from a list: remove element lis Main> remove 2 [3,4,5,2,7] [3,4,5,7] remove element [] =[] remove element list = if element = = head list then tail list else (head list ):(remove element (tail list)) Main> remove 2 [3,4,5,2,7,2,2] [3,4,5,7,2,2] remove element [ ] = [] remove element list = if element = = head list then remove element (tail list) else (head list):(remove element (tail list)) Main> remove 2 [2,3,2,4] [3,4] Main> remove 4 [4,3,5,4,4%¢%4¢pf ® Usts 3 یی

صفحه 9:
More pattern matching remove element [ ] =[ ] remove element (first:rest) = if element = = first then remove element rest else first:(remove element rest) Main> remove 3 [4,5,3,7] [4,5,7] Main> remove ‘a’ ['a','b','c'] "bo" Main> remove "Joe" ["Jack","Andy"] ["Jack","Andy"] Lecture 8 Lists 9

صفحه 10:
Other Example functions on lists --length of a list lengthList [ ] = 0 lengthList (x:xs) = 1 + lengthList xs Main> lengthList [10,21,3,14,54] 5 --take two lists and make a list of pairs Main> myZip [11,21] [34,46,65] [(11,34),(21,46)] myZip xs []=[] myZip [ ] ys =[ ] myZip (x:xs) (y:ys) = (x,y) : myZip xs ys Main> myZip [1,2,3] ["Mary","John","George"] [(1,"Mary"),(2,"John"),(3,"George")] Lecture 8 Lists 10

صفحه 11:
A trace of lengthList lengthList ] [ = 0 lengthList (x:xs) = 1 + lengthList xs lengthList [10,21,3,14,54 1 + lengthList [21,3,14,54 1+ (1 + lengthList [3,14,54]) 1+ (1+ (1 + lengthList [14,54] )) 1+ (1+ (1 + (1 + lengthList [54] ))) 1+ (1 + (1 + (1+ (1 + lengthList [])))) 111 ‏د‎ )1 311 0((ر(‎ 5 ۱۱ ۱ ۱ ۱ ۱ ۱ | Lecture 8 Lists 11

صفحه 12:
ther examples of functions using pattern matching product [ ] = 0 product [a]=a product (a:x) = a * product x sum[]=0 sum (a:x) = a + sum x reverse []=[] reverse (a:x) = reverse x ++ [a] head (x:_) =x tail (_:xs) = xs Lecture 8 Lists 12

صفحه 13:
Main> member 3 [1,2,3,4] True member :: Int-> [Int] -> Bool member e [ ] = False membere (x:xs) |e= =x = True |otherwise = member e xs Main> member 3 [4,2,5,6] False Main> member 'a' ['b','a','g'] ERROR - Type error in application #* Expression _: member ‘a' ['b','a','g'] ۴ Term :['b','a‘;'g'] ۳ Type : [Char] > Does not match : [Int], re 8 Lists 13

صفحه 14:
]5 guesses the type when you don’t declare it member e [ ] = False membere (x:xs) | ‏دادع‎ = True jotherwise = member e xs Main> member ‘a’ ['a','b‘,’c’] True Main> member "mike" ["joe","peter","andrew"] False Main> member 1 [1,2,4,6] Lecture 8 Lists 14 True

جهت مطالعه ادامه متن، فایل را دریافت نمایید.
34,000 تومان