welcome back everybody to
the Anna lecture to
about types and clash and
this part we're going to talk about types of
functions and since we are dealing with functional programming
and functions values Antibes
are very important and since this
can be a little bit and difficult for beginners
police pay attention to this lecture in particular biggest
if you missed this you will have difficulties
in all their remaining lecture so and I will go a little bit slow here
and repeat many times because this is super important
that you understand this part monitor function
a function is a mapping from values
of one type to values of another time show here too
very simple examples a function types the first one
is the function not ligation which takes Boolean
and breeders in Other Boleyn an
not to nothing to end complicated
let's look at the second function here is digit
is digit takes a character and checks
writer that character is digit show whether that character is 0-1
up to nine the diver that is chargeable shell
what that means is it takes a value in the shower all characters
and will return a value in this ad of booleans
and in general a function from t1 to t2
is a the shark dive team on a roadie to
is the type of functions that map values
from typed the wrong two values for
dude of type t2 the one ish
also sometimes called the domain of the function
and d2 is sometimes called their range
the function
now you noticed that and free rides
the arrow of the function dive as a nice arrow
but when you use the key part and to type in the actual code
you will type that as a dash followed by a greater than sign
and but we will use it can I search index here but just you
be careful this is not you know how you type in
actually the arrow when you have a function
just like grits the doubles embed lists
that typed for the range in the domain are
unrestricted for example we can ever function here
at that takes as the argument a top all
of two integers and returns as a result another integer
so at up X camara y equals x plus why so you see here that
at takes it up all to of two integers
and returns a result that is an integer and the other French in here
an is a function that takes and then and returns a list of integers
I'm perfectly fine and
definition their $0.02 an readers the list
0 dot dot and we have seen in the very first lecture
that this square bracket with the an
double dots denote the list from zero up to an
one thing
every make the types
did arguments and result function types
arbitrary they can be functions themselves and this is where
current functions common so instead of defining at
to take double-a integers and return integer
and has college more common to define at as follows:
we define at as a function that takes an integer
and readership function that takes another integer
and returns an integer so
look at this type here so at is a function
here we see that from this era
that takes an integer and returns a function
that takes another integer and readers an integer
said that is curry now
by itself carrying is not
very complicated if you need you now there functions are just
values like list an integer show there's nothing special about this
the thing words becomes confusing is that
in Haskell you don't have to ride dish arrows here
alright because this is so common and
the arrow associates to deride
and which means that you can write this diverse end
arrow and arrow and and that's where it becomes confusing for began
show you have to really gonna you know when you read these types know where to
put the brand
she's but for now it you now there's nothing special here
at is a function that takes an integer and readers another function that takes
an integer and then returns an integer again
now noticed that both at
and at prime return the same result
okay but at takes the
the normal at stake should %uh polisher takes both arguments at the same time
where's at Brian takes the argument
one by one and arguments that they can
that punches take their arguments one by one are called
current functions an and
the person that invented
this an way of dealing with functions where the functions take the arguments
one by one
boss has Kobe curry and Haskell
is named after him so that's where has gone from just like Pascal
and was named after blaise pascal
Haskell is named after haskell be curry
and the person that invented carrying of functions
every function that takes two arguments:
can be turned into a function that takes these arguments one by one
for example here's a function multi that takes three
parameters XY and z. and multiply stem some old
XYZ is X time July dime si
one way to ride this function would be to take a top all
three values XY and z. and then multiplied
but the Haskell way of doing this is to take these arguments one by one
so mold axe readers a function
that takes another argument why and then
its readers a function that takes another argument see
and then why you have all these three arguments you can multiply them
to return that and final and
okay so moult takes an integer
readers a function multi axe addiction and azure why
that readers a new function mold actual I that takes into Jersey
and readers the result XY and z.
now what we see here is that
and it died we still ride to Brenda she showed a brand that she's in the type
go left to right so function the function arrow
binds to the right but
when we ride here and nafta gonna go here when we write mold
XY we don't put branch 3.2 per
would put a branch there you would ride mold axe
Iran's and then branch around that you there the brand she's
associate to the left a chauffeur application
the parentheses show she ate to the left for the function arrow
they are associate to the right and that matches up perfectly show then
in practice we don't have to write and the brand
now you can ask yourself
why is this guy really useful okay
why are we just like in other languages
why don't we just
have functions that take a whole list of parameters are
like top of parameters to be precise in one go
why do we do this complicated thing of God taking one our game and and then
returning a function that takes the other
parameter et cetera well the reason is
that we can partially apply a function
for example if we have this function at Brian
that was defined in a curried way we can define a function that
incur a man's fill you with Wong by partially applying
at crime to wong so if we look at that type there
at prime Wong is a function that takes an integer
everyday urge another integer when we will talk about last
in the future lecture we'll see that there's many functions
that transform lists into other lest
and Moshe dysfunctions are also defined in the current form
for example the function take here take five
and take is defined as a current function
that takes an integer readers a function from ledge to list
which may change that we can partially applied
take 25 to yield a function
that takes a list of integers to another list of integers
which just takes the first five Allemands
of the input list similarly the function drop
takes an integer and readers a function from less two lists
and what we see here is that we can now define dropped five
we can partially applied drop 25 and it will return a function
that will take a list dropped the first five elements
and then returned should this is super convenient
and and an
its in Haskell it's very lightweight you disinfection haskell
is optimized to have carried functions
and in that respect haskell is very very different from
most other languages that are optimized for functions that take
to boss a bargain I already mentioned a couple of times
the way we do and the breadth rent a seizure on
and expressions with function parameters so the
arrow associates to the right so if we have the
and the their type
on the bottom day here so the type here into arrow and
arrow in Tarrant the brand she's our
showcase do the ride and says we don't like to write parentheses
began ride this thing as indeed arrow in Tarrant arrow in
okay but if we put all the practices and their
we get the term here at the bottom but this is what we gonna ride
and I guess we we don't want a ride
unnecessary for entities and as I said all saw
when we do function application the
and Brenda sheesh Associated the left
so when we partially apply a function you can write Brent this is our on dad
but we don't have to chill when you write mold XYZ
that really means mall tax applied
to why apply to G okay
but an unless
and Angela unless you're using explicit
the bus all functions haskell are normally defined
encourage for an an most languages
all functions are defined in
and the encourage for so I'll
dish the part 2 and about functions there will be
a lot of exercises to drill this idea of you know where to put the brand she's
in the and function type
and where to put the brand she's in the expressions
and so this become second nature is this something you really have to
gonna keep doing until you don't think about it then
alright so an please do your exercises
and see you later and in the last part
of Chapter two