hey welcome back to our lecture on list comprehension
and what we're going to do an
and the second part is talk about a
and a function called zip that we can use
to combine two lists into
a single list and and here's the signature for Sep shows zip
as type lester Bay to
Lester B to list of bears
of a call might be and here's an example of zipping through less together
and and there you can see why this
function is called Sep it takes two lists and it takes
each value and combines them in a bear
so ship of ABC and want to 34
is a long be too c3
and the zip stops as soon as one of the lists
is exhausted so in this case you see there's four elements on the right left
but only three elements on the left lest and the resulting list
has three elements and as I mentioned in one of the early lectures
I think the signature of ship
would be a little bit nicer if it was not courage if he would ride
where a blessed of a list gonna be
to list of pair of a commodity
and but this is just the way
ship is defined in the Haskell standard library
I'm using ship
weekend and the fine the following function
that takes a list and creates bear shove
every adjacent value so if we have let's first look at the
an example down here before we look at the implementation because I think when
you see the example the implementation
is fairly obvious so what we want to do is given 1234 we wanted an upgraded
chain
like a sliding window of and bears
want to 23 34 so you see that
you know did do is change their site won't do
23 NN to treat 34 so how can we do that
and dish function here is
and I think a beautiful
example of and using zip
and at to do that so what you do.
is I take that list 1234
then I a good day love this left
234 and then I ship those two together show
now want to get this century starts the first this is 1234
and the second place this dude 34 so now when we ship it we get to jury
and then when we ship the next wrongly get sorry I'm Egypt that we get one too
the next is 23 and the Nexus 34
write a review if you withdraw
the picture you get the last you drop the first element
shift that one up and then you ship that too and this
is exactly what you get alright
am here's another function that
and uses the function pairs
to check whether a list shortage so what we do is we
and take the all the adjacent pairs in the last
sorry dad it will give us here extra my wife's
and then recheck that the first value is smaller than the second
the list is sorted if if we look at every adjacent pair of elements
the first one is less than the second so this is a super concise
I'm way to check whether list is short show let's just look at some examples
is wanted 34 shorted yes it is begosh
one comment to long this lasts into an annex Paris to commit three
do is smaller than three and final pair streak on my for
artery is less than four so at that list is sorted
an is 123 24 sordid
no because the second pair in
bears of this blessed is to bear
32 and their 3 is
greater than 2 so that will return false
using ship
we can also define
the positions of a.m. an element analyst
as I mentioned before am lists dog have
any position unlike erasure in an array you can
index into the array an but lists you cannot read less you can only access the
elements
from an left to right so I can take the hat
and then the had of the day line and had said
and Dale detail a chat room an
but sometimes its convenience to also have the position
of each element analyst so this is a fairly common
operation and the way we do that is using ship
so what we do as we take the list excess
we take an the list from zero to an
and where and if the length of this list but this
really can be the infinite less starting at zero an
and then we just ship the
and the alleged am
wit the positions to get a.m.
every element here
alright so if we look at the a.m. and
am positions here
we get depositions of 0 well
does Rd the cities and %eh so what we did beef what we did is we first zipped
every element with its position and then we felt there are two only the values
that we were looking for because here where only
and dresses in the positions %uh 0
and and so and this think goes into steps we first
bear every element to it its position
and then we felt their odds only the values that we were looking for
am up there in Haskell strings are nothing more than syntactic sugar for
list of characters
which means that you can I use comprehension
over list as well so for example here's a string
and ABC and which
is really nothing more then the list of characters
a be and she and hands all operations on list
including comprehension but also map filter
Ford are all there lest abrasions that we'll see
will work over and strength and
for jump on the length of abcd as five
and why is that because that string is just a list
of five characters and showed issues is
the length pension over list we can take the first three
an characters a blessed by you using take three sorry got ABC
and we can ship a list a Sharia string
where the list an and this way so for example every ship
ABC with 1234 we get the list of pairs
where the character a Wong character be too
and character see with three and many other languages
also have strange emblem and
enter a baller enumerator so you can do
an the same thing and but in Haskell really
there is no special type for less its
defiant as a list of characters and the only thing is dead
there's a special literal syntax just like
this index for lists really as we have seen means Wong
comes to on three cons for const on the entry list
and here's an example
where we an can't count
the and number %uh lower case characters
in a string so what we do as we say
give me all the characters axe in a string access
filter out all the launch that are lower case
an and then computer the link
so for example if we counted the number of lower case characters
and a string has car we see that there are six
to for 6 and then
the first character isn't a brick a symbol
alright hopefully and
you've got a little bit of a feeling of
list comprehension and zip and
and the fact that strings are less and
and the exercises there will be plenty of opportunity
to and practice with that she next week
bye bye happy hacking