welcome back am
and the first part up this a lecture
we at seen some simple examples of
how to define functions and has gone especially how that
differs from most contemporary language saw
we have seen that in Haskell conditionals are expressions not
statements
but thats haskell programmers rather ride
guarded equations than conditional expressions
the other thing we have seen is how did he find
how to define functions using better matching
and we have seen in particular how to define functions over lists
using pattern matching well we're going to look at now
is how to define functions biggest has caused a fun show language
so we have to have a way a concise way
to define functions and that Islam the expressions
and you probably know /url on the expressions
a.m. from the language that you're using a pure Java programmer
in Java eights and along the expressions were injured used
if you're sheesh our program error you no longer expressions
if your hack or PHP programmer
you have along the expressions if your javascript you have loved expression
lund expressions are
everywhere she plus plus you name it d
and swift every mother language has long expressions and as we saw on the ferry
first lecture
edition inventions from along to church
in the 1930s and
most languages lund expressions look kinda similar
and has called a look as follows: so
day am looked like this
on the axe arrow body of the lawn expression
now and actual codes that you die you don't write alumni
deride the slash the right slash X
and is set up the arrow derides and
minus sign and greater than but since we want these lights to look fancy
where
actually riding along than and a real
bank but when you ride this thing called the right slash
for the London and you just right the arrow
as you would expect K so what is this thing
this is a function addiction X and ad said
to itself and that function has no name so it's an expression
a function type thats Rutland expressions are
they are expressions that the note functions
Joanne
the in in mathematics and and
as church designed them are the ideal
read along the darkness let richard eyre international on that but we tied them
as
backslash and instead of the arrow symbol
we ride the aris him again
but for the rest haskell I think it's a language that is closest to the lawn the
cartilage
because it's based on your functions version
and other languages lund expressions are
I think a better way would be to call them closures
and we will see why because they can close over variables
in the outer scope but the the slum the expressions can have side effects
and but as we said it doesn't really matter
we are really emphasizing the fact that functional programming is
programming using expressions so why are London expressions useful
well they're useful because they allow you to express your intent better
when you're currying functions let's look at this first example here
and but SL arriving at XY
because X plus why should this occur redefinition and
this is a function that they should parameter axe
readers a function that takes a barometer why
and then returns X plus Y really
the intent is better expressed by this
at is a function
it takes bram interact readers a function that takes a brother why
and then add them together okay
so this is actually syntactic sugar
fordice and when I write haskell I often write them in this for
even though it's a little bit longer
at means that I have to type more characters
but I think this for is more readable than that for
am and in the course of
of this these lectures we'll switch between
both of these and but again if you look at my haskell God
I prefer to write things using a splash at long last to convey
the actual meaning of the current function and expressions
are also useful when you want to and
make explicit that you're returning a function that's
a camp some sense the same scurrying but here
Ikea if we define a constant function
so this is a function and given a will return a function that will never be
you gave it will return that day so %eh defined goals backs
readers a function that will whatever you give its just ignore it
and return X okay so that constant function there
is very concisely defined using along the expression
where's here I think it's a little bit less clear
gone still backs underscore equals x
so this round doesn't make explicit defect
that you're returning a function so he is also emphasized by putting the
parentheses
around died and here you don't do that
so I'll this is how I were dry dysfunction I would
with the brand she's on the type and I puts the
writer function as a lawn the expression am
here's another you shove using on the expressions
and that's to avoid inventing names
inventing names is
one of the hardest things that we have to do it programming in
coming up meaningful names because often
you know their language forces you to name something where you just don't want
to name it just want to bash the thing directly
so for example if I want to map a function
over a list there's no reason I should get that function here
name and what this have say anyway
so instead to what I can do again just
Obama's love the expression do the map
and that will it be used to map this function
over the list right show this
not idiomatic national this
idiomatic really the only way
place are you want to name something is when you use it more than once
or if it becomes clear but thats
yeah up to interpretation here's another
all the tea of Haskell and that's called sections
so with sections you can take
a binary operator in and fix operate like plus
and use it as a function so by putting
a ranch around it you can use plus as a function so one plus two
it was three and plus in brands
12 is also tree now this is a nice trick
because we can also foods Wong
a brand one-plus and we can do
plus upper and blessed to show weekend section began
partially apply plus by putting
Iran's around the plush so here we see
we can do one-plus which is a function
that expect second argument I will
at 12 it and here we have to function
section plus do which is a function that expects
value here and then added
to ensure this is one place to equals three show in general
F
is a by a plus funny place is a binary operator
then you can ride plus and brands
to use it as a normal function
you can left or right section head by
partially applying it to an argument K
sewer this is something that you will see while all the National
specially here if you want to have a function that anchormen something
just right one-plus as a section to wire sections useful
well here are a couple of examples you on the function that God fiction
argument and hot shit to all
you just ride opened brand divided by two
and you don't have to ride lumber axe
axe divided by two so sectioning allows you to write something
read-out inventing an and thats
said the whole goal of this exercise
giving names is hard so we want to avoid that
and Section allows you to do that alright
so I am here's a preview of the exercises the exercises will be
available on the website
and these exercises real ask you
to use am some of these constructs that
a.m. we have seen in this lecture to define
functions and as I promise you this will be the last
and exercise that's very haskell specific
but since you now we we have been talking about how haskell
differs from other languages I think it's quite useful
an to follow the rest of the lectures to do these exercises
to get a little bit familiar an
with the Haskell syntex am and so happy hacking
and see you next week bye bye