1 00:00:01,030 --> 00:00:04,990 hey welcome back to our lecture on list comprehension 2 00:00:04,990 --> 00:00:08,320 and what we're going to do an 3 00:00:08,320 --> 00:00:11,750 and the second part is talk about a 4 00:00:11,750 --> 00:00:15,650 and a function called zip that we can use 5 00:00:15,650 --> 00:00:18,910 to combine two lists into 6 00:00:18,910 --> 00:00:23,640 a single list and and here's the signature for Sep shows zip 7 00:00:23,640 --> 00:00:26,840 as type lester Bay to 8 00:00:26,840 --> 00:00:29,970 Lester B to list of bears 9 00:00:29,970 --> 00:00:34,739 of a call might be and here's an example of zipping through less together 10 00:00:34,739 --> 00:00:37,899 and and there you can see why this 11 00:00:37,899 --> 00:00:41,210 function is called Sep it takes two lists and it takes 12 00:00:41,210 --> 00:00:44,980 each value and combines them in a bear 13 00:00:44,980 --> 00:00:48,570 so ship of ABC and want to 34 14 00:00:48,570 --> 00:00:51,570 is a long be too c3 15 00:00:51,570 --> 00:00:55,690 and the zip stops as soon as one of the lists 16 00:00:55,690 --> 00:01:00,890 is exhausted so in this case you see there's four elements on the right left 17 00:01:00,890 --> 00:01:05,850 but only three elements on the left lest and the resulting list 18 00:01:05,850 --> 00:01:10,670 has three elements and as I mentioned in one of the early lectures 19 00:01:10,670 --> 00:01:13,860 I think the signature of ship 20 00:01:13,860 --> 00:01:17,990 would be a little bit nicer if it was not courage if he would ride 21 00:01:17,990 --> 00:01:21,140 where a blessed of a list gonna be 22 00:01:21,140 --> 00:01:24,240 to list of pair of a commodity 23 00:01:24,240 --> 00:01:27,299 and but this is just the way 24 00:01:27,299 --> 00:01:30,490 ship is defined in the Haskell standard library 25 00:01:30,490 --> 00:01:33,659 I'm using ship 26 00:01:33,659 --> 00:01:37,880 weekend and the fine the following function 27 00:01:37,880 --> 00:01:41,520 that takes a list and creates bear shove 28 00:01:41,520 --> 00:01:46,229 every adjacent value so if we have let's first look at the 29 00:01:46,229 --> 00:01:50,259 an example down here before we look at the implementation because I think when 30 00:01:50,259 --> 00:01:52,460 you see the example the implementation 31 00:01:52,460 --> 00:01:57,899 is fairly obvious so what we want to do is given 1234 we wanted an upgraded 32 00:01:57,899 --> 00:01:58,570 chain 33 00:01:58,570 --> 00:02:02,159 like a sliding window of and bears 34 00:02:02,159 --> 00:02:05,780 want to 23 34 so you see that 35 00:02:05,780 --> 00:02:09,289 you know did do is change their site won't do 36 00:02:09,289 --> 00:02:14,010 23 NN to treat 34 so how can we do that 37 00:02:14,010 --> 00:02:17,010 and dish function here is 38 00:02:17,010 --> 00:02:20,519 and I think a beautiful 39 00:02:20,519 --> 00:02:24,010 example of and using zip 40 00:02:24,010 --> 00:02:27,269 and at to do that so what you do. 41 00:02:27,269 --> 00:02:30,689 is I take that list 1234 42 00:02:30,689 --> 00:02:34,620 then I a good day love this left 43 00:02:34,620 --> 00:02:38,650 234 and then I ship those two together show 44 00:02:38,650 --> 00:02:43,349 now want to get this century starts the first this is 1234 45 00:02:43,349 --> 00:02:47,650 and the second place this dude 34 so now when we ship it we get to jury 46 00:02:47,650 --> 00:02:52,109 and then when we ship the next wrongly get sorry I'm Egypt that we get one too 47 00:02:52,109 --> 00:02:55,730 the next is 23 and the Nexus 34 48 00:02:55,730 --> 00:02:59,069 write a review if you withdraw 49 00:02:59,069 --> 00:03:02,989 the picture you get the last you drop the first element 50 00:03:02,989 --> 00:03:06,569 shift that one up and then you ship that too and this 51 00:03:06,569 --> 00:03:11,040 is exactly what you get alright 52 00:03:11,040 --> 00:03:14,379 am here's another function that 53 00:03:14,379 --> 00:03:18,220 and uses the function pairs 54 00:03:18,220 --> 00:03:22,299 to check whether a list shortage so what we do is we 55 00:03:22,299 --> 00:03:25,970 and take the all the adjacent pairs in the last 56 00:03:25,970 --> 00:03:29,439 sorry dad it will give us here extra my wife's 57 00:03:29,439 --> 00:03:34,000 and then recheck that the first value is smaller than the second 58 00:03:34,000 --> 00:03:39,209 the list is sorted if if we look at every adjacent pair of elements 59 00:03:39,209 --> 00:03:43,819 the first one is less than the second so this is a super concise 60 00:03:43,819 --> 00:03:49,359 I'm way to check whether list is short show let's just look at some examples 61 00:03:49,359 --> 00:03:53,120 is wanted 34 shorted yes it is begosh 62 00:03:53,120 --> 00:03:58,009 one comment to long this lasts into an annex Paris to commit three 63 00:03:58,009 --> 00:04:02,669 do is smaller than three and final pair streak on my for 64 00:04:02,669 --> 00:04:05,909 artery is less than four so at that list is sorted 65 00:04:05,909 --> 00:04:10,180 an is 123 24 sordid 66 00:04:10,180 --> 00:04:13,319 no because the second pair in 67 00:04:13,319 --> 00:04:16,430 bears of this blessed is to bear 68 00:04:16,430 --> 00:04:19,459 32 and their 3 is 69 00:04:19,459 --> 00:04:22,909 greater than 2 so that will return false 70 00:04:22,909 --> 00:04:27,240 using ship 71 00:04:27,240 --> 00:04:29,460 we can also define 72 00:04:29,460 --> 00:04:33,450 the positions of a.m. an element analyst 73 00:04:33,450 --> 00:04:37,800 as I mentioned before am lists dog have 74 00:04:37,800 --> 00:04:41,140 any position unlike erasure in an array you can 75 00:04:41,140 --> 00:04:46,810 index into the array an but lists you cannot read less you can only access the 76 00:04:46,810 --> 00:04:47,580 elements 77 00:04:47,580 --> 00:04:51,240 from an left to right so I can take the hat 78 00:04:51,240 --> 00:04:54,640 and then the had of the day line and had said 79 00:04:54,640 --> 00:04:58,010 and Dale detail a chat room an 80 00:04:58,010 --> 00:05:01,860 but sometimes its convenience to also have the position 81 00:05:01,860 --> 00:05:05,650 of each element analyst so this is a fairly common 82 00:05:05,650 --> 00:05:09,680 operation and the way we do that is using ship 83 00:05:09,680 --> 00:05:13,290 so what we do as we take the list excess 84 00:05:13,290 --> 00:05:17,500 we take an the list from zero to an 85 00:05:17,500 --> 00:05:20,570 and where and if the length of this list but this 86 00:05:20,570 --> 00:05:24,670 really can be the infinite less starting at zero an 87 00:05:24,670 --> 00:05:27,990 and then we just ship the 88 00:05:27,990 --> 00:05:31,110 and the alleged am 89 00:05:31,110 --> 00:05:34,790 wit the positions to get a.m. 90 00:05:34,790 --> 00:05:37,950 every element here 91 00:05:37,950 --> 00:05:41,490 alright so if we look at the a.m. and 92 00:05:41,490 --> 00:05:44,520 am positions here 93 00:05:44,520 --> 00:05:47,930 we get depositions of 0 well 94 00:05:47,930 --> 00:05:53,270 does Rd the cities and %eh so what we did beef what we did is we first zipped 95 00:05:53,270 --> 00:05:59,080 every element with its position and then we felt there are two only the values 96 00:05:59,080 --> 00:06:02,330 that we were looking for because here where only 97 00:06:02,330 --> 00:06:05,570 and dresses in the positions %uh 0 98 00:06:05,570 --> 00:06:12,130 and and so and this think goes into steps we first 99 00:06:12,130 --> 00:06:15,310 bear every element to it its position 100 00:06:15,310 --> 00:06:19,840 and then we felt their odds only the values that we were looking for 101 00:06:19,840 --> 00:06:26,840 am up there in Haskell strings are nothing more than syntactic sugar for 102 00:06:28,030 --> 00:06:29,310 list of characters 103 00:06:29,310 --> 00:06:33,680 which means that you can I use comprehension 104 00:06:33,680 --> 00:06:36,890 over list as well so for example here's a string 105 00:06:36,890 --> 00:06:39,600 and ABC and which 106 00:06:39,600 --> 00:06:42,600 is really nothing more then the list of characters 107 00:06:42,600 --> 00:06:47,020 a be and she and hands all operations on list 108 00:06:47,020 --> 00:06:50,040 including comprehension but also map filter 109 00:06:50,040 --> 00:06:54,060 Ford are all there lest abrasions that we'll see 110 00:06:54,060 --> 00:06:58,560 will work over and strength and 111 00:06:58,560 --> 00:07:02,180 for jump on the length of abcd as five 112 00:07:02,180 --> 00:07:05,640 and why is that because that string is just a list 113 00:07:05,640 --> 00:07:09,360 of five characters and showed issues is 114 00:07:09,360 --> 00:07:13,290 the length pension over list we can take the first three 115 00:07:13,290 --> 00:07:18,780 an characters a blessed by you using take three sorry got ABC 116 00:07:18,780 --> 00:07:22,930 and we can ship a list a Sharia string 117 00:07:22,930 --> 00:07:27,050 where the list an and this way so for example every ship 118 00:07:27,050 --> 00:07:30,890 ABC with 1234 we get the list of pairs 119 00:07:30,890 --> 00:07:34,240 where the character a Wong character be too 120 00:07:34,240 --> 00:07:39,160 and character see with three and many other languages 121 00:07:39,160 --> 00:07:42,340 also have strange emblem and 122 00:07:42,340 --> 00:07:45,840 enter a baller enumerator so you can do 123 00:07:45,840 --> 00:07:49,180 an the same thing and but in Haskell really 124 00:07:49,180 --> 00:07:52,260 there is no special type for less its 125 00:07:52,260 --> 00:07:56,650 defiant as a list of characters and the only thing is dead 126 00:07:56,650 --> 00:08:00,240 there's a special literal syntax just like 127 00:08:00,240 --> 00:08:04,520 this index for lists really as we have seen means Wong 128 00:08:04,520 --> 00:08:08,610 comes to on three cons for const on the entry list 129 00:08:08,610 --> 00:08:12,750 and here's an example 130 00:08:12,750 --> 00:08:16,490 where we an can't count 131 00:08:16,490 --> 00:08:20,920 the and number %uh lower case characters 132 00:08:20,920 --> 00:08:24,390 in a string so what we do as we say 133 00:08:24,390 --> 00:08:28,320 give me all the characters axe in a string access 134 00:08:28,320 --> 00:08:31,430 filter out all the launch that are lower case 135 00:08:31,430 --> 00:08:34,680 an and then computer the link 136 00:08:34,680 --> 00:08:39,110 so for example if we counted the number of lower case characters 137 00:08:39,110 --> 00:08:42,500 and a string has car we see that there are six 138 00:08:42,500 --> 00:08:46,110 to for 6 and then 139 00:08:46,110 --> 00:08:49,680 the first character isn't a brick a symbol 140 00:08:49,680 --> 00:08:52,300 alright hopefully and 141 00:08:52,300 --> 00:08:55,890 you've got a little bit of a feeling of 142 00:08:55,890 --> 00:08:59,160 list comprehension and zip and 143 00:08:59,160 --> 00:09:02,230 and the fact that strings are less and 144 00:09:02,230 --> 00:09:05,690 and the exercises there will be plenty of opportunity 145 00:09:05,690 --> 00:09:09,650 to and practice with that she next week 146 00:09:09,650 --> 00:09:10,960 bye bye happy hacking