hey welcome everybody
tune in next lecture of from some programming 101 axe
today we're going to talk about list comprehension
and this comprehension siege are
a mechanism to write code that manipulates collections
and and every look at most programming languages
most of the features in a programming language
has to do wit dealing with collections if you look like
a traditional imperative language judge's job our C sharp
an it has a wide range of mechanisms to deal with connection
form the while loops do while loops
foreach loops contain you break
and all these constructs are there to
it to rate over collections and
now of course mathematicians have been dealing with collections
an for for centuries and
and for mathematicians the favorite
collection type is sets and
sets however are computationally a little bit awkward
because when you have a set image that duplicated
elements are removed which implies that you need a quality to delete chechan
especially in a functional language like has gone
and it's very hard well and possibly have to solve the halting problem
2d decide equality between functions
so what
haskell did it took some of the ideas that meant mission mathematicians have
used
for a long time to deal with chats and
simplified them to work over lists analysts are
my favorite collection died and and the reason is delish
just gives you didn't bare minimum to deal with the collection
you can as you have seen if we have a list that's composed out of a hat and a
tail
you can decompose that less recursively but just picking off the first element
one by
well and that's very convenient and short bridge
very few restrictions on the collection time
unless statuary how are you issue quality of the elements or a race where
you assume
am constant I'm indexing so Schatz
are in some sense the most your form
of collection types and the job on a job for eight
day am gold these lists streams
and to emphasize dejar
collections where you can only xsd:element
one by one and in that sense
has call lists are an example of a job for eight streams in that philosophy
alright thats
and look at how mathematicians to use
so-called set comprehension to define sets
and here's an example a.m. a base at
dead it sits at the mall squares an
which and values
between one and five show is its X where'd
where axe is from this they confirm this at 125
so the result of this is dis at 149 1625 and
but as i sad I'm every take
ax to range over functions then this becomes really hard to implement
so what people dead and
in functional languages is take the same notation the same ideas
but instead of sets usual ish and show SATCOM branches for mathematics
become list comprehension and Haskell
and here's that same example region as a list comprehension so here you see
it's the list of square numbers
X square to wear action is taken from the list
want to fight a year you see this year redesigned axe
is taken from the less than 125 and
if you evaluate this you will see that
this returns the list 1 for 9 16 25
but is alleged not set
the expression
X taken from want to buy that's called a generator
an and the reason it's called a generator because it
defiance on how to generate value Sh
Forex writer axis taken from that shit
I'm of course answers everything
and has call and AR that's a good
aspect of programming language has to be compositional so you should be able to
just you know recursively use the same structures over and over again
and comprehension scan have multiple generators
and here you see an example of that
so we attic all pairs x-com I Y
Rx is drawn from one to three and why
is drawn from for Fife and what you see
is dead the why various
faster then DX show the result list is
14 15 to
425 3435 an
as another way to look at ID snatched it
generators are multiple generators is like
and nested loop where do why is the NRA nope
and the axe is the outer loop so if we change if we show up the order of the
generator
then now and X
very smart witty biggest this becomes the envelope so what we get now
is am but notice that we still
have here extra mile i show we only show up the order
of the generators not the order of the very most
so now instead of an and
this guy moving fast as you get
X moving fast so the result is 14
two for 34 et cetera and
and this notion of generators
an you will recognize dead
in many many programming language showing haskell these are less go
branches if you use by Tom
and also as list comprehension an
skyline has forgone branch ins at sharp edge comprehension
and C sharp in Visual Basic an
half as link comprehension
am and if you look at the language like she call
which is completely built and on top of comprehension
only dare you don't ride em this concise a year deride select from where
a jet dry but it's basically the same idea so by studying
and let's go branches and has gone you will be well equipped
to use the same ideas in whatever language you encounter them in
alright and
now
we have seen now a few simple examples for generators
where the generators only had Constance but
generators can also deep and on each other
and just like when you have a nope inner loop
can use variables from the outer nope and Red Gum branches
that's no different show every look here
we say pictures drawn from wanted three and then
why is drawn from X 23
so axes and scope in the generators that
gone after it and every executed dis comprehension
we get the latest an one long
ego-c I Y is drawn from one to three
because action starts out and wong so we get 11 12 13
than an why is exhausted show
we're going to checked the next value action that will be too
so now why is taken from two to three so easy to
to 23 and then the last iteration
we take xtube eatery and then why is drawn from the list
323 shown at is the last value is just
3 comments re am but again your
totally familiar read this because this is the same and how nested loops work
and any language showed the inner loop can
use variables defined and outer loops
and one of the nice things
of comprehension and especially of and the band generators
is that you can ride very concise God
for example an if we want to take a list of lists
and flatten them into a single list an
we can do that an with the following comprehension
so each day just give me every list out-of-date lists of lists
and then just give me every element of that list
and return that so this is a at EE nested loop that goes over
every last in this list a blessed and just
yields all the values to return
a single-edged of type a up the same element type
as the originally saw by me
and concatenate 123 45 and six
the result will be on 23 456
right Jul very very
gone size code and that we can ride using comprehension now the next thing
this I generators that you want to use in a comprehension and that's also
and use a lot inset comprehension is to allow filters
you want to and you now
have X drawn from some collection
but you want to draw out certain values
forage a predicate does not hold over job but we want to have
all the even numbers between one and 10
we can just say drawing X is drawn from wanted an
and filter out only the even numbers so the result
of this is a list 246 aids done
the even numbers between long and then
and and its sequel you will recognize this as a select where
so the WHERE clause here an is called a guard
in ask our and if you use a link
and list comprehension czar and comprehension syntax debt will be also
be called
where
here's a am
example an that uses guards to take
number and break it down into its factors
so what are the factors well we take a number and
we take all the numbers between 1 and then
an and then recheck we only
retained oesch forage and modulo actually 0
so defectors were 15 sure we start out with all the numbers between
1 and 15 and then we'd throw out all the numbers
for rich it and modulo axe is not here are
and in this case defectors are 13 5 and fifty
and then we can use this
factors an function
to defined a very simple
am predicate to check whether numbers prime
numbers prime if defectors have that number are exactly the list Wong and an
and for jon bon five is not a prime
because defectors are not just one and fifteen
as one three five 10 15 but seven
is a prime because you can check by using Ghei are your
on favorite programming language that defectors are seven are one and seven
and so we decide that
7 as a prime okay
so everyone to not check whether
an in our what are the primes up to a certain number
well weekend ride this simple let's go branches are we say
give me all the numbers between two and then were and that's the brand matter to
dish
function and then I only want to can't maintain the prime numbers I filter out
all the numbers that are not branch over I want to have
all the prime numbers and up to 40
an I just run this and list comprehension
and I get an all the prime numbers now of course this is not the most efficient
way
to compute prime numbers but I think it's a very nice illustration
views English comprehension so see you
and the next chapter