1 00:00:01,020 --> 00:00:03,060 hey welcome everybody 2 00:00:03,060 --> 00:00:07,350 tune in next lecture of from some programming 101 axe 3 00:00:07,350 --> 00:00:11,480 today we're going to talk about list comprehension 4 00:00:11,480 --> 00:00:14,660 and this comprehension siege are 5 00:00:14,660 --> 00:00:19,270 a mechanism to write code that manipulates collections 6 00:00:19,270 --> 00:00:23,070 and and every look at most programming languages 7 00:00:23,070 --> 00:00:26,900 most of the features in a programming language 8 00:00:26,900 --> 00:00:31,510 has to do wit dealing with collections if you look like 9 00:00:31,510 --> 00:00:37,020 a traditional imperative language judge's job our C sharp 10 00:00:37,020 --> 00:00:41,750 an it has a wide range of mechanisms to deal with connection 11 00:00:41,750 --> 00:00:45,160 form the while loops do while loops 12 00:00:45,160 --> 00:00:48,649 foreach loops contain you break 13 00:00:48,649 --> 00:00:52,480 and all these constructs are there to 14 00:00:52,480 --> 00:00:56,280 it to rate over collections and 15 00:00:56,280 --> 00:01:01,320 now of course mathematicians have been dealing with collections 16 00:01:01,320 --> 00:01:04,540 an for for centuries and 17 00:01:04,540 --> 00:01:07,659 and for mathematicians the favorite 18 00:01:07,659 --> 00:01:11,500 collection type is sets and 19 00:01:11,500 --> 00:01:15,979 sets however are computationally a little bit awkward 20 00:01:15,979 --> 00:01:19,240 because when you have a set image that duplicated 21 00:01:19,240 --> 00:01:24,509 elements are removed which implies that you need a quality to delete chechan 22 00:01:24,509 --> 00:01:27,520 especially in a functional language like has gone 23 00:01:27,520 --> 00:01:32,000 and it's very hard well and possibly have to solve the halting problem 24 00:01:32,000 --> 00:01:35,439 2d decide equality between functions 25 00:01:35,439 --> 00:01:38,460 so what 26 00:01:38,460 --> 00:01:43,290 haskell did it took some of the ideas that meant mission mathematicians have 27 00:01:43,290 --> 00:01:44,000 used 28 00:01:44,000 --> 00:01:47,280 for a long time to deal with chats and 29 00:01:47,280 --> 00:01:52,820 simplified them to work over lists analysts are 30 00:01:52,820 --> 00:01:57,810 my favorite collection died and and the reason is delish 31 00:01:57,810 --> 00:02:01,619 just gives you didn't bare minimum to deal with the collection 32 00:02:01,619 --> 00:02:06,860 you can as you have seen if we have a list that's composed out of a hat and a 33 00:02:06,860 --> 00:02:07,450 tail 34 00:02:07,450 --> 00:02:12,970 you can decompose that less recursively but just picking off the first element 35 00:02:12,970 --> 00:02:13,569 one by 36 00:02:13,569 --> 00:02:17,030 well and that's very convenient and short bridge 37 00:02:17,030 --> 00:02:20,139 very few restrictions on the collection time 38 00:02:20,139 --> 00:02:25,129 unless statuary how are you issue quality of the elements or a race where 39 00:02:25,129 --> 00:02:26,079 you assume 40 00:02:26,079 --> 00:02:30,209 am constant I'm indexing so Schatz 41 00:02:30,209 --> 00:02:34,010 are in some sense the most your form 42 00:02:34,010 --> 00:02:37,840 of collection types and the job on a job for eight 43 00:02:37,840 --> 00:02:41,900 day am gold these lists streams 44 00:02:41,900 --> 00:02:44,919 and to emphasize dejar 45 00:02:44,919 --> 00:02:47,930 collections where you can only xsd:element 46 00:02:47,930 --> 00:02:51,079 one by one and in that sense 47 00:02:51,079 --> 00:02:56,489 has call lists are an example of a job for eight streams in that philosophy 48 00:02:56,489 --> 00:02:59,790 alright thats 49 00:02:59,790 --> 00:03:03,409 and look at how mathematicians to use 50 00:03:03,409 --> 00:03:07,159 so-called set comprehension to define sets 51 00:03:07,159 --> 00:03:10,949 and here's an example a.m. a base at 52 00:03:10,949 --> 00:03:14,799 dead it sits at the mall squares an 53 00:03:14,799 --> 00:03:18,150 which and values 54 00:03:18,150 --> 00:03:21,599 between one and five show is its X where'd 55 00:03:21,599 --> 00:03:25,299 where axe is from this they confirm this at 125 56 00:03:25,299 --> 00:03:30,689 so the result of this is dis at 149 1625 and 57 00:03:30,689 --> 00:03:34,159 but as i sad I'm every take 58 00:03:34,159 --> 00:03:39,409 ax to range over functions then this becomes really hard to implement 59 00:03:39,409 --> 00:03:43,060 so what people dead and 60 00:03:43,060 --> 00:03:46,949 in functional languages is take the same notation the same ideas 61 00:03:46,949 --> 00:03:52,259 but instead of sets usual ish and show SATCOM branches for mathematics 62 00:03:52,259 --> 00:03:55,280 become list comprehension and Haskell 63 00:03:55,280 --> 00:04:01,290 and here's that same example region as a list comprehension so here you see 64 00:04:01,290 --> 00:04:04,449 it's the list of square numbers 65 00:04:04,449 --> 00:04:08,009 X square to wear action is taken from the list 66 00:04:08,009 --> 00:04:11,150 want to fight a year you see this year redesigned axe 67 00:04:11,150 --> 00:04:15,109 is taken from the less than 125 and 68 00:04:15,109 --> 00:04:18,500 if you evaluate this you will see that 69 00:04:18,500 --> 00:04:22,460 this returns the list 1 for 9 16 25 70 00:04:22,460 --> 00:04:27,600 but is alleged not set 71 00:04:27,600 --> 00:04:28,520 the expression 72 00:04:28,520 --> 00:04:32,300 X taken from want to buy that's called a generator 73 00:04:32,300 --> 00:04:36,120 an and the reason it's called a generator because it 74 00:04:36,120 --> 00:04:39,919 defiance on how to generate value Sh 75 00:04:39,919 --> 00:04:43,690 Forex writer axis taken from that shit 76 00:04:43,690 --> 00:04:47,330 I'm of course answers everything 77 00:04:47,330 --> 00:04:50,580 and has call and AR that's a good 78 00:04:50,580 --> 00:04:54,280 aspect of programming language has to be compositional so you should be able to 79 00:04:54,280 --> 00:04:59,160 just you know recursively use the same structures over and over again 80 00:04:59,160 --> 00:05:03,380 and comprehension scan have multiple generators 81 00:05:03,380 --> 00:05:06,600 and here you see an example of that 82 00:05:06,600 --> 00:05:10,090 so we attic all pairs x-com I Y 83 00:05:10,090 --> 00:05:13,500 Rx is drawn from one to three and why 84 00:05:13,500 --> 00:05:16,990 is drawn from for Fife and what you see 85 00:05:16,990 --> 00:05:20,169 is dead the why various 86 00:05:20,169 --> 00:05:24,229 faster then DX show the result list is 87 00:05:24,229 --> 00:05:27,470 14 15 to 88 00:05:27,470 --> 00:05:31,110 425 3435 an 89 00:05:31,110 --> 00:05:34,460 as another way to look at ID snatched it 90 00:05:34,460 --> 00:05:38,139 generators are multiple generators is like 91 00:05:38,139 --> 00:05:41,450 and nested loop where do why is the NRA nope 92 00:05:41,450 --> 00:05:48,450 and the axe is the outer loop so if we change if we show up the order of the 93 00:05:48,470 --> 00:05:49,349 generator 94 00:05:49,349 --> 00:05:52,690 then now and X 95 00:05:52,690 --> 00:05:57,060 very smart witty biggest this becomes the envelope so what we get now 96 00:05:57,060 --> 00:06:00,130 is am but notice that we still 97 00:06:00,130 --> 00:06:03,860 have here extra mile i show we only show up the order 98 00:06:03,860 --> 00:06:07,190 of the generators not the order of the very most 99 00:06:07,190 --> 00:06:11,650 so now instead of an and 100 00:06:11,650 --> 00:06:15,030 this guy moving fast as you get 101 00:06:15,030 --> 00:06:18,889 X moving fast so the result is 14 102 00:06:18,889 --> 00:06:22,440 two for 34 et cetera and 103 00:06:22,440 --> 00:06:25,789 and this notion of generators 104 00:06:25,789 --> 00:06:28,820 an you will recognize dead 105 00:06:28,820 --> 00:06:32,550 in many many programming language showing haskell these are less go 106 00:06:32,550 --> 00:06:34,590 branches if you use by Tom 107 00:06:34,590 --> 00:06:38,080 and also as list comprehension an 108 00:06:38,080 --> 00:06:42,750 skyline has forgone branch ins at sharp edge comprehension 109 00:06:42,750 --> 00:06:46,390 and C sharp in Visual Basic an 110 00:06:46,390 --> 00:06:49,550 half as link comprehension 111 00:06:49,550 --> 00:06:53,500 am and if you look at the language like she call 112 00:06:53,500 --> 00:06:57,500 which is completely built and on top of comprehension 113 00:06:57,500 --> 00:07:01,950 only dare you don't ride em this concise a year deride select from where 114 00:07:01,950 --> 00:07:06,270 a jet dry but it's basically the same idea so by studying 115 00:07:06,270 --> 00:07:10,550 and let's go branches and has gone you will be well equipped 116 00:07:10,550 --> 00:07:15,020 to use the same ideas in whatever language you encounter them in 117 00:07:15,020 --> 00:07:18,550 alright and 118 00:07:18,550 --> 00:07:21,610 now 119 00:07:21,610 --> 00:07:25,900 we have seen now a few simple examples for generators 120 00:07:25,900 --> 00:07:29,500 where the generators only had Constance but 121 00:07:29,500 --> 00:07:33,010 generators can also deep and on each other 122 00:07:33,010 --> 00:07:36,550 and just like when you have a nope inner loop 123 00:07:36,550 --> 00:07:40,490 can use variables from the outer nope and Red Gum branches 124 00:07:40,490 --> 00:07:44,360 that's no different show every look here 125 00:07:44,360 --> 00:07:47,680 we say pictures drawn from wanted three and then 126 00:07:47,680 --> 00:07:50,860 why is drawn from X 23 127 00:07:50,860 --> 00:07:54,780 so axes and scope in the generators that 128 00:07:54,780 --> 00:07:59,710 gone after it and every executed dis comprehension 129 00:07:59,710 --> 00:08:04,000 we get the latest an one long 130 00:08:04,000 --> 00:08:07,210 ego-c I Y is drawn from one to three 131 00:08:07,210 --> 00:08:12,300 because action starts out and wong so we get 11 12 13 132 00:08:12,300 --> 00:08:16,320 than an why is exhausted show 133 00:08:16,320 --> 00:08:20,419 we're going to checked the next value action that will be too 134 00:08:20,419 --> 00:08:24,570 so now why is taken from two to three so easy to 135 00:08:24,570 --> 00:08:28,310 to 23 and then the last iteration 136 00:08:28,310 --> 00:08:32,610 we take xtube eatery and then why is drawn from the list 137 00:08:32,610 --> 00:08:35,860 323 shown at is the last value is just 138 00:08:35,860 --> 00:08:39,360 3 comments re am but again your 139 00:08:39,360 --> 00:08:44,720 totally familiar read this because this is the same and how nested loops work 140 00:08:44,720 --> 00:08:48,310 and any language showed the inner loop can 141 00:08:48,310 --> 00:08:51,950 use variables defined and outer loops 142 00:08:51,950 --> 00:08:55,600 and one of the nice things 143 00:08:55,600 --> 00:08:59,870 of comprehension and especially of and the band generators 144 00:08:59,870 --> 00:09:02,959 is that you can ride very concise God 145 00:09:02,959 --> 00:09:07,040 for example an if we want to take a list of lists 146 00:09:07,040 --> 00:09:10,970 and flatten them into a single list an 147 00:09:10,970 --> 00:09:14,450 we can do that an with the following comprehension 148 00:09:14,450 --> 00:09:19,190 so each day just give me every list out-of-date lists of lists 149 00:09:19,190 --> 00:09:22,649 and then just give me every element of that list 150 00:09:22,649 --> 00:09:27,560 and return that so this is a at EE nested loop that goes over 151 00:09:27,560 --> 00:09:31,190 every last in this list a blessed and just 152 00:09:31,190 --> 00:09:34,529 yields all the values to return 153 00:09:34,529 --> 00:09:38,620 a single-edged of type a up the same element type 154 00:09:38,620 --> 00:09:42,019 as the originally saw by me 155 00:09:42,019 --> 00:09:46,430 and concatenate 123 45 and six 156 00:09:46,430 --> 00:09:49,589 the result will be on 23 456 157 00:09:49,589 --> 00:09:53,910 right Jul very very 158 00:09:53,910 --> 00:10:00,250 gone size code and that we can ride using comprehension now the next thing 159 00:10:00,250 --> 00:10:04,540 this I generators that you want to use in a comprehension and that's also 160 00:10:04,540 --> 00:10:09,180 and use a lot inset comprehension is to allow filters 161 00:10:09,180 --> 00:10:12,350 you want to and you now 162 00:10:12,350 --> 00:10:16,019 have X drawn from some collection 163 00:10:16,019 --> 00:10:19,089 but you want to draw out certain values 164 00:10:19,089 --> 00:10:22,610 forage a predicate does not hold over job but we want to have 165 00:10:22,610 --> 00:10:26,050 all the even numbers between one and 10 166 00:10:26,050 --> 00:10:30,339 we can just say drawing X is drawn from wanted an 167 00:10:30,339 --> 00:10:34,399 and filter out only the even numbers so the result 168 00:10:34,399 --> 00:10:38,440 of this is a list 246 aids done 169 00:10:38,440 --> 00:10:41,870 the even numbers between long and then 170 00:10:41,870 --> 00:10:46,170 and and its sequel you will recognize this as a select where 171 00:10:46,170 --> 00:10:50,220 so the WHERE clause here an is called a guard 172 00:10:50,220 --> 00:10:53,600 in ask our and if you use a link 173 00:10:53,600 --> 00:10:58,300 and list comprehension czar and comprehension syntax debt will be also 174 00:10:58,300 --> 00:10:59,149 be called 175 00:10:59,149 --> 00:11:02,380 where 176 00:11:02,380 --> 00:11:03,690 here's a am 177 00:11:03,690 --> 00:11:07,920 example an that uses guards to take 178 00:11:07,920 --> 00:11:11,750 number and break it down into its factors 179 00:11:11,750 --> 00:11:15,230 so what are the factors well we take a number and 180 00:11:15,230 --> 00:11:18,980 we take all the numbers between 1 and then 181 00:11:18,980 --> 00:11:22,880 an and then recheck we only 182 00:11:22,880 --> 00:11:26,360 retained oesch forage and modulo actually 0 183 00:11:26,360 --> 00:11:30,360 so defectors were 15 sure we start out with all the numbers between 184 00:11:30,360 --> 00:11:34,430 1 and 15 and then we'd throw out all the numbers 185 00:11:34,430 --> 00:11:38,240 for rich it and modulo axe is not here are 186 00:11:38,240 --> 00:11:42,420 and in this case defectors are 13 5 and fifty 187 00:11:42,420 --> 00:11:45,440 and then we can use this 188 00:11:45,440 --> 00:11:49,339 factors an function 189 00:11:49,339 --> 00:11:52,740 to defined a very simple 190 00:11:52,740 --> 00:11:56,390 am predicate to check whether numbers prime 191 00:11:56,390 --> 00:12:02,230 numbers prime if defectors have that number are exactly the list Wong and an 192 00:12:02,230 --> 00:12:05,589 and for jon bon five is not a prime 193 00:12:05,589 --> 00:12:09,510 because defectors are not just one and fifteen 194 00:12:09,510 --> 00:12:12,560 as one three five 10 15 but seven 195 00:12:12,560 --> 00:12:17,420 is a prime because you can check by using Ghei are your 196 00:12:17,420 --> 00:12:21,550 on favorite programming language that defectors are seven are one and seven 197 00:12:21,550 --> 00:12:24,650 and so we decide that 198 00:12:24,650 --> 00:12:28,170 7 as a prime okay 199 00:12:28,170 --> 00:12:31,830 so everyone to not check whether 200 00:12:31,830 --> 00:12:35,810 an in our what are the primes up to a certain number 201 00:12:35,810 --> 00:12:39,490 well weekend ride this simple let's go branches are we say 202 00:12:39,490 --> 00:12:43,589 give me all the numbers between two and then were and that's the brand matter to 203 00:12:43,589 --> 00:12:44,070 dish 204 00:12:44,070 --> 00:12:49,089 function and then I only want to can't maintain the prime numbers I filter out 205 00:12:49,089 --> 00:12:51,579 all the numbers that are not branch over I want to have 206 00:12:51,579 --> 00:12:55,520 all the prime numbers and up to 40 207 00:12:55,520 --> 00:12:59,600 an I just run this and list comprehension 208 00:12:59,600 --> 00:13:04,290 and I get an all the prime numbers now of course this is not the most efficient 209 00:13:04,290 --> 00:13:05,200 way 210 00:13:05,200 --> 00:13:10,190 to compute prime numbers but I think it's a very nice illustration 211 00:13:10,190 --> 00:13:13,459 views English comprehension so see you 212 00:13:13,459 --> 00:13:14,309 and the next chapter