hi everybody a gum
to another app is out of at the
100 max and what we're going to do today
is we're going to look how to define
functions in Haskell nom
I think everybody %uh view that
has been to college in a different town
them where they grew up has had
this experience you know in one way or another
you leave your own down where you grew up he went to
elementary school you went to highschool you go off for college
and then ten years later you come back
and thinks have changed slightly
but they're also a little bit the same
and that can be a little bit disorientating because you know
your memory thanks you know were
like they were but a reality they have changed
slightly and well
you whilst get the same experience when you look
at haskell haskell looks very familiar
to other programming languages again differs in subtle ways
and goal of this lecture is to
walked through some of the syntax of has goal
and function definition and expressions
and compare and contrast the wit
a.m. how you do these things and other programming languages
and once you're aware of these differences
it will be fairly easy to adopted them but
there are some things where haskell is definitely a little bit
different than an what
most people are used to in Java or
in BHP or whatever your favorite languages
so on
the first thing is conditional expressions and every language has
conditional expressions am and often there's a difference between conditional
statements
that are yet ready using if they announce and conditional expressions
turn their expressions that are written in red question mark
golan and other languages do have if their nails for expressions at all for
example in Scala
but if you're using C sharp or
job on there there's the conditional statements
and then for conditional expressions there's
different index right haskell since there's no statements
an when you want to ride a conditional you ride
if than else nation this
gauge or defining a function addiction integer
and breeders another integer and its defiant
Adonis is the absolute value sure it takes
value as as if that value is greater than 0
then you return an else you return
minus and you'd negate value so here you see that we're using the familiar
if the house as an expression for an
of course since it's just an expression
they can be and nested as well
and sensed conditions conditions are expressions
you always have to have a Dan and else part
a ride so in this case you can define
and the sign of a number save and is less than 0
its -1 it and is zero
and assigned 0 and else and and is greater than zero then the sign
is one alright so you always have to have both
then and else branch just like in turney expressions
I'll an
because has killed people like brevity
they often don't use conditional expressions but the you show called
guarded equations
and the guard integration is
kept her common pattern where a function starts with a conditional saw
if the right hand side over function definition immediately has a conditional
it's often nicer to use this form with guard integration sure you write your
function definition your left hand side
and then you're right you're conditions here so
the absolute value of an is
an when and is greater than equal r0
arch minus an otherwise and otherwise is just
another alias for false now let's
go back to and let's go back to
definition here up the absolute value using conditional
and here you see what I said is that this is a function
this defiant immediately using
a conditional and
and haskell people prefer to ride it like this and I see that the right hand
side
is simpler and the cases are
more distinguished you going i'm easily reason to buy this
its greater than 0 if it's not
it its am less than zero and the other function especially when you have nested
conditionals
this becomes even more worthwhile
so in order to define the sign up a number average less than zero
-1 its equal 200
an otherwise is Wong here you see
they got mathematical heritage of has call
where we like to write things in a very
concise way and these guarded equations
are something that are very
idiomatic has so typically you don't ride
if they announce you ride an
these guarded equations
another thing that and
is very heavily used in Haskell
and that few contemporary languages
have its better matching an
and scholar for example has better matching
there you have to use a a case statement to do to better matching
and she sharp will get better matching is uses the switch statement
but it has cool you can use better matching directly do when you define
function so for example when we define
want to define negation function from ball
tubal we can define a bit better matching directly showing against not a
false
it was true and not of through it was false
John that is very very concise
definition negation let's look at another example
say that we want to define a.m. and
of two billion show ditch a function for bald eagle
to belong and we just ride on
that through table for and so this is
exactly how are you if you if you look up the definition
of and and any book on digital logic you will see the same through table here
so we can ride a dime but if you look at
all of this you see that it can be written
even more compactly because the only case
where this thing is true is run
both arguments here are true again
and in all other cases it's false so you can take tish
these four cases here and collapse them down to DC do
through and through it goes through and otherwise
whatever and whatever it was false
and this underscore here is used
very very often has car and it means
this is a very bold I don't care about is just use as a placeholder
and doing better match again
and now if you look at this definition
an it well evaluates
both arguments and
in order to do this match because we're putting a pattern
on both sides of D and
has good people are very sensitive to this because has collision lazy language
in
they want you want to do as little a valuation as possible
in order to an finds
the answer and to
dysfunction call alright so I a nicer definition
according to like haskell ATM is a full-line
if if it's true then
through and something is the same as B
and false and whatever is false
let's see if this is true through
and threw it goes through through and false
Eagles fall so through and whatever's there
is whatever was there an average
falls on the left hand side it doesn't matter what's here on the right inside
edge
always falls saw this is actually
they gonna your them most
up the mall definition of
and because it only
evaluates the first re-invent and
as necessary
not haskell patterns are matched in order
saw there let matched top to bottom left to right or left to right
top to bottom if I'm really precise and
and so the compiler does not try to rearrange
the patterns necessarily to am
make tomorrow the mall biggest the order
is important and you can enter makes a better matching
red-carded equations and so
the easiest thing to think about is that that there's are always matched
left to right top to bottom
you cannot 3b variables for example
you cannot say B&B egos be
and assured that this implicitly doing equality
overby Asia although variables
inside a pattern must be unique an
one thing that we have seen we have written lists using describe records
and describe records really are just indexing sugar for
and writing the list using explicit
gongs and and the list
right so analyst appears in a in
a constant list appear some someplace
it will mean this but inside a better matching
we often have to write lists in this for so for example when we want to define
at the head of a list redefined using better matching as follows:
in the right to list in Gaunce had are in had
still form thats and call it like that so
the hat of analysts redhead action and Dale
I don't care is the hat and detail
up a list with hats that I don't care about and deal access
is excess I'm so again
very concise definitions and just for fun
let's look at the types they'll take Celeste
and since that day Lord blesses Celeste it
diapers list
to list hat take Celeste
but that value of the head
is the element type of the less its a this thing goes from list
day to a now of course
these are not total functions biggest had until
are not defined on the empty list so when I apply had stood empty list
there's no pattern that matches
and so what you will get as you'll get an error and the same for tail
another way to say that
is that the pattern like this ex-cons excess and only matches
a non-empty sure if you go ahead with the empty list
this pattern here
doesn't match because it's not the form
ex-cons anything okay
so an easy had a venti less there's no case that matches
you get an error the other thing
is that you have to and brent is sized is less
patterns because remember application by its strongest
so this would mean has of axe calms
yarn a.m. anything so you have to put brands
am in these batters to make it work alright
that was our first part of defining functions national
see you after the break