ever ready welcome back to
ft 10 on ax functional programming
an and over granted
to do today is taking the first
baby steps in learning haskell now
one of my favorite movies of all time
is the Karate Kid and if you haven't seen this movie
stop this yeah watching
this lecture and go watch this movie first
every time I need to learn something new I rewatched the Karate Kid
and I can tell you I've seen it you know maybe
20-30 times and every time i watch it
I learn something new if you're
a watch the movie you will see that
the and the boy here Danielle and wants to learn karate
and mister me I got it the gentlemen here
I'm on the other side and make him do a lot of
what and Danielle things are pointless exercise any gets kinda frustrated
but in order to learn something you have to go
through a lot of exercises because there's love muscle memory or and
case of programming you have to have the muscle memory in your fingers they're
certain things should not have to think about
that you have to drill that you have to do over and over and over again
and that's what we are going to do in this lecture today
don't get frustrated don't be like Daniel
be a grownup and just do your exercises
on and on wax on wax off wax on wax off
until it becomes completely automatic
and then you can use your brain to solve the real problem
you don't have to think about the basics any okay and every time you learn
something new
re watch this great movie alright
and show some ask has complementation that we're using
in this course a is the Glasgow Haskell Compiler
sometimes also called the glorious Haskell Compiler
> is the leading implementation of Haskell
and it has both a compiler an interactive
Rep a loop that you can use to typing your
got and the interactive nature
of the GATT interpreter makes it really easy
to experiment and with has car
and that's why it's also very useful for discourse
am many of the exercises
that you will get during the course you can do
by using ght I don't care I don't
see that as cheating an because
as programmers reshoot really use to compiler
to help us okay so the computer is there to help us programmed the computer
if we don't use that that would be stupid GT
is freely available from that website here there will be also be on the
an course website sorry you can also
Google or Bing for the Haskell platform
and it's available for all major platforms
Mac when does and Linux to start GHz
you just type from the command line Ghei
and then you get some
text blur and there you see a bronze
preludes and that's where you can start typing an
expressions for example we can type in expression to
plus three times for and result if that's the
Ghei will then printed the result for that in this case were really using
GA CIS gotta day calculator
it's a little bit I can now calculate their own a command line interface
and set up a girl getting a red buttons well we also see here's that since
to last three times for apparently age 14
that an not obligation bind stronger
then addition as so there's no surprises there
most languages have that and everyone to
at first duty addition and multiplication we have two boots
a.m. brand to see a show we ride to blustery
I'm for and then there is all this trendy and we can do
more complicated expressions like the square root of dead three to the power
of two
for to the bar of two and we get five am
as you saw there was is it gonna be a the prelude
and and from
here so and at school prelude it's not the SDK for Haskell these are all the
standard functions
that come for free and what has gone on the website
you will we will tell you how to get other libraries for Haskell
but an rewards discuss that in too much detail right here you
you can read up on that we own will
will try to stake with just this standard library functions standard
values
and most of them are most influential order to function in the prelude
are functions over lists am
and we're going to get walked through a couple of them and then later lectures
we well implemented these functions in several different
manners the first function years had which takes a list
and the way the right mission has college with square brackets and the
elements separated by commas
showed ahead of wanted 3-for-5 s
and surprisingly Wong the tail
that's the opposite of Hatcher it is everything on the list
accept that so detail wanted 3-for-5
is the list to treat for we can also select
and the and element of the list which this year
double action mission mark operators are if we want to select the second element
of the list
I wanted 3-for-5 we get
3 so what do we learn from this well
that the first element of the list
as index 0 K so
like it should be haskell starch
indexing lists by zero now there's
to exclamation marks here and there's
a good reason for that biggest in Haskell
lists are not to raise show indexing
into the analyst here at the second element
will traverse the Leisure World reversal estro a long
throw a to and then returned three so this is not a constant I'm
operation to indexing into a list unlike induction into an array
and Haskell is then they ran the length of the list
are and then there and and this number but in general linear in the size of the
list
and but fortunately
an reveal hardly see uses
of a this operator in practice and
if you find yourself wanting to use less than texting
then you know that you have not done than of wax on wax off wax on wax off
you're still too much in the mindset of
impaired if programming using a race and
and Haskell we typically use higher order functions like Matt filter fault
and not indexing to a.m.
perform complex operations over list here's another
an example take three it takes the first three element of a list so if we have
the list went 3-for-5
guess what readers the first three elements
123 drop
is the opposite side takes a number
and the list and it drops the first three elements of the list so in some
sense drop
and take are
the generalizations of head and tail at entail
just take the first and the rest of the last and then which day can drop
you can you now you can generalized that too arbitrary numbers
and calculating the length of the last
alright we do that by saying Lancs
the going to function length length wanted 3-for-5 is five
and again and lesson at school are not like a race
in other languages so taking the length is not a constant I'm operation
it is takes let its time linear in the length of the list
and some sums up all the elements show every goal some unwanted 345
the result is fifty product
is the same as some acceptance data adding up the numbers
multiplies all of them so want to multiplying long
to three four and five like taking effect or l5
is 120 offending two lists is another common operation you take to less the
smashing together into a new list so I
a band 123 with four and five
we get the less wanted three for $5 and the loss function here
on this and slides is reversing a list
reverse 1,2,3,4,5 it's you
5 for 321 in the rest of this
these lectures we will define these all the functions ourselves
in several ways an but right now
it's good to kind of play with them to see what they do
an and then we will be ready to implement them
an in our then we will know other supposed to do
here's the
and another very important slight
and that is about function application because has girl
is very different than mathematics on the one side
and very different from other programming languages as well
in mathematics when you apply function mathematics that you do in school or
college when you take analysis
when you apply a function to an argument
you always use parentheses so in this case
when we apply a function here this is
applying a function have to arguments a NB
and then adding C
space D now in mathematics
typically art and that's gone from physics
is that space mange multiplication
right so if you look at the mathematical textbook
and you see ya see space D
usually need see multiplied by D
and therefore when you apply a function you're using
Prentice's like this to to disambiguate since in Haskell
function application is used much more often
then multiplication virus in physics
and multiplication is used more often so that's where
not obligation is given the Kaneohe the most whites involved at least gonna
visible symbolic motion white spaces Mr and Haskell
function application is denoted by white space
so that it can if it's not so much in your face
when haskell and celebrating at of a call might be
plus she space D function application becomes
denoted by a spatial its after apply to a
apply to be plus see times the
alright and most programming languages
you would ride at for an
a go might be goes for so they are in some sense the world
worst of two worlds their are your your have to write everything explicit
the National we make that decision that function application
is done by using a space and multiplication
is done by using multiplication
and also
function application by stronger
then any other operator so if you see af
of a place be that means after applied to a
plus be doesn't mean af
applied to a-plus be game disc and get
yeah people have to get used to this this doesn't mean
after dimes a-plus be it means
F apply
to a blessed be so that's
got similar to multiplication binding stronger than addition
okay here's a table that give you sum up the
am problem more expressions
and on that one side's haskell
and on the other side mathematics so this is what you should forget
I'm going to stand in front of that this is what you should forget
and this is what you should learn alright
so forget what's behind me and learn
what's on this site okay so
instead of after opened brand actually write
at SpaceX instead of passing two parameters to at
ripon we apply have to action and then apply that revealed to why
instead of at opened brand GOP brown actually can ride
at of geoVax and later we'll see
that we can write that using function composition
have as as at combos G okay
and their and the rest up the examples
should be obvious okay but if you just look at
shapes have these two don't try to interpret them you will see that the
Haskell code here
looks much lighter weight than the code
on this side and Haskell programmers are lazy we don't want to dive too much
so this looks much more beautiful and this
and that's why Azkals index is designed the way it is
thank you this was the end of Part long
see you in a bit for the remainder of
kept their own