good Monday morning I am mpj and this is
fun fun function this is a series where
we learn Haskell together
I don’t know Haskell we are learning it
from the book learn you a Haskell
there’s a link to it in the description
below if you just stumble across this
video you might want to watch the
episodes from the beginning by clicking
there in this episode we will be looking
at a segment of the book called baby’s
first functions linked in the episode
description this segment of the learn
your Haskell book talks about defining
functions and this is how you do it
double me x equals x plus x now we have
a function and I can call double me 5
and that will give us 10 we’re learning
somebody in the comments complained
about me using a lot of paper in my
episodes so in honor of that person I
bought these huge ass papers and these
huge pens anyway you have the function
name here and this is the parameter and
this is a function body I’m going to
copy this and bam I just saved that as a
file called baby age as and according to
the book this is how you load it so it’s
like : l and then baby we are not gonna
do that because that’s insane I want to
write out load ok compiling main may be
age as interpreted and okay modules
loaded main what I didn’t Y mean I
didn’t define main anywhere uh maybe
that’s some default mode if you know
that right down in the comments either
way I think we can now call the the
function yes I’m not get intellisense
with with tab that’s pretty cool so I
can call double means X and I get 12
does that work with with yes it does so
next up the book wants us to do a
function called double us and it’s gonna
take two arguments x and y equals x
times
to our plus y times two and the book
just not what I want spaces I will load
three seven that’s twenty maybe okay
yeah well trust Haskell not
unsurprisingly you can call your own
functions inside of of other functions
so I can go double me X and double sobel
double double
I can’t hide double me and I can do that
so you see that I’m not return here and
here’s this this is the same thing I’m
blowing your mind here but joking aside
this is a pretty good example of a
pattern that you will be seeing use a
lot in Haskell combining functions more
specifically making basic functions that
are obviously correct and combining them
into more complex functions and we do
this in order to avoid repetition this
is not a new concept old programming
languages habit but because Haskell is
so crazy terse just look at this it
allows becomes sort of supercharged in
Haskell just to give you an idea about
how little syntax it it requires in
Haskell to define a function I’m just
gonna go and implement this quick link
Java so you’re gonna write a public
Clause because everything in Java needs
to be a class my math utils lalala and I
also like public public static void know
is it’s gonna end my bubble me and it’s
int and it’s gonna take X it’s going to
be point and it’s going to return X plus
X so look at that
this is I’m gonna pull this up for a bit
so you see it
this is 88 characters and this is say
one at 18 characters so Java syntax time
is four hundred eighty percent for high
schools and ducks and remember that both
of these are statically typed languages
in JavaScript this would be a double
double me ax X plus X and you would have
to say constants well or possibly bar to
be nice if I had said that yeah these
like look how much shorter JavaScript is
than Java you could have said that
there’s all kinds of typing information
here that adds add safety but in the
Haskell case Haskell actually has a
strong static type system just like Java
does the difference is that Haskell in
first these things for us so we don’t
have to declare them explicitly like we
have to do in Java and yes your people I
know that this comparison has several
problems but I need to make the point
that Haskell has a very low function
overhead what I mean by that is that it
is a lot cheaper to create a function in
Haskell compared to most other languages
related to this I want to mention
another episode I made on arrow
functions in JavaScript because I think
that there is a lot of Haskell in the
arrow functions in JavaScript you can
check out that episode buck lucky there
but we’re doing Haskell here so let’s
delete this Java and JavaScript you
can’t go back to Haskell by the way it
doesn’t matter which order you define
these functions so I can take this here
and I can move it up here and I can load
baby again and I can call double us vii
what does this error even mean
Interactive 9 1 error no instance for
show a 0 arrow
a serie arising from the use of print
maybe you haven’t applied a function to
enough argument oh wait a minute wait a
minute oh it was actually helpful
seven eight thirty I just called it with
seven here and I needed to call it with
seven and eight because well it has has
two arguments but the point that I was
going to make was that even though we
define double as here we can refer to
double me in this function here even
though that is defined on line two now
we’re gonna take a look at if-then-else
so we’re gonna write a function called
double small number which is going to if
the number that we give it is smaller
than 100 it’s going to double it very
useful so it’s gonna take the number at
the argument X the number X and if if X
is over 100 it’s just going to then just
don’t return X else it’s going to be x
times two let’s load that and we’re
gonna call small number and we are going
to say 101 it’s going to return 101
because that’s not a small number but if
we call double small number we say 7
it’s going to double it so you’ve seen
an if/else before by a subtle thing in
Haskell is that else is mandatory it
simply must return something there’s no
not in JavaScript for instance you could
just omit the else so I could do a
function double small number in
JavaScript and I more like if the number
X is below 100 I could return with your
neighbor to Richard x times two
and if it wasn’t to here it would just
go to the next line and he would return
undefined which might be a nasty
surprise for the person using the
function double small number so if
JavaScript was Haskell the compiler
would have forced you to do something
like this I’m going to delete this
javascript code by the way you could
write this on one line is to show you
that that is that it’s perfectly
possible most small number 714 still
works but dividing into multiple lines
is I think it’s pretty nice the book
talks about the fact that this is an
expression so what’s an expression ah
let’s see expression examples so an
expression is simply a piece of code
that would turns a value so 5 is an
expression that will change the value 4
plus 8 that’s an expression X plus y
that’s also an expression and now this
is also an expression because this will
always return a value that’s why you
need the else because if it didn’t have
the else this would not really be an
expression it would not always return
value this is also where parents come in
let’s say that we wanted to add 1 to the
value that we doubled let’s load that
oops
I am parse error because yeah this this
is not well it has let’s delete all that
it’s loaded again alright and I call
double small number 6
that’s 13 it doubles it and then it adds
1 but if I called double small number
101 we expect this to be 102 or we want
this to be 102 but it’s still 101
because it’s actually evaluating like
this right if X is 100 it’s going to
return X or else it’s going to
x times 2 plus 1 so if you wanted to add
the 1 either way we need to add friends
here and that is what makes this a
single expression we use the parens to
denote that this is an expression in the
book they suggest adding this to the
function name this is a bit funky to me
I’m like it’s gonna load that and then
you call double small number and this is
not a special character in Haskell it’s
a completely valid thing to have in a
function name but the way I understand
it is that you tend to add these when
you have a function that is it slightly
modified version of something so in this
case I guess that we add it because you
know it’s it’s doubling the small number
kind of almost if I don’t quite
understand the need for this convention
yep
if you have an insight on this please
comment down below
but either way these are like you could
and and that should work right it should
be noted here that we cannot do this I
think so if I do know that I get some
weird error they just constructor Conan
O’Brien not in scope so functions have
to kind of begin with a capital letter
the book says that it will talk about
why later and I guess that it will have
to do with these data constructors maybe
and we should also look at the fact that
this doesn’t take any function
parameters this is a function that is
just this expression this
a function that will always return this
string it Sammy Conan O’Brien to the
JavaScript part of my brain this looks a
lot like a variable or I guess I a
constant because I it’s not possible to
redefine it so I assume that in Haskell
maybe you don’t use constants at all you
just use this functions that are defined
to values in Haskell functions like this
function said don’t take any parameters
they are referred to as definitions or
sometimes you just call them names the
Ruby part of my brain it kind of draws a
parallel to symbols
I’m sure if that’s correct that is the
end of us reading or learning the baby’s
first functions segments of the book
learn you a Haskell if you want to try
this on your own there’s a link in the
description to this exact segment of
we’ll learn you asked for book next time
we will be looking at lists do not miss
that you have just watched an episode of
fun fun function I release this every
Monday morning if you liked this episode
you should click the channel below and
check out if there are other episodes
that you like and and there will be and
so you should subscribe
I am mvj until next Monday morning thank you