welcome to this jam session
and then this jam session I'm going to show you
how the principles functional programming that we've seen
in the course for Haskell can be applied
a.m. to their language cop Gotland
is a language designed to buy JetBrains
and if you're in and ellijay user like I am
and you can and download the plugin for cropland
and and play with a child let's go and messy
how we can do functional programming in
a a normal language an
here is the Anna and main
function for our call in program just like
job on sharper than made
takes array of strings as an argument and
inside the body we see a couple of expressions here
and I want to walk you through them and and show you
how this relates today and Haskell code
that we have seen and notes that for me
functional programming does not imply
and not using side effects functional programming does not mean
using pattern matching for me functional programming means
using higher-order functions whether they're you're not
an and focusing on expressions
various statements and got then you can define you to both they're both so I can
define
var and why and
initialized tu wong and then I can say why
and plus equals Wong
and this just work and I have no problem at all with that
okay but what I wanted to show here
is and the functional programming features
that are supported by cop so let's look at this for statement here so I define
a an immutable variable here are the
just a name a square and that goals
the an function foo and Fu is defiant
here as a function you see the and
if I over over this it and that may be a little bit
small to see I'm on the screen but and got then has type inference for the
return type show here
it said Tekken that's very small yellow thing that you
currency that the type of
through and is a function that they can
and & Returns & and here I
call through a forty-two and I printed now another thing that we've seen in
Haskell
and the less comprehension ce work these range expressions where we
we could ride in Haskell square brackets Wong dot dot
am an got and support something very similar so I can say for action
at the range 125 print X
so here you see that this shame gonna syntax and I the years
range comprehension appear
in and other languages as well now
the next example shows
am some higher order functions and the syntax that got land uses for them to
hear
I declare lest a list of the values 1234
and then I mapped the function a
I rode along that expression a a row eight times to over it
and then I filter out all the values
less than five and you see that there's two ways to define
along the expression the first one here is where you name the
arguments and arrow and then the expression body
and the longer is enclosed in curly braces
you can leave out the
an Iran's around the map
but I can have liked that to put them there
explicitly and hear the filter is seeded if
a long the expression just takes one argument we don't have to name it
and we just call it it's very similar to
when you have an object that the and implicit
context that this parameter is called this
when you have a function with one parameter
it make sense to call that one parameter give it a special name
such that you have to write less
okay sure you can do everything that you learned in the course
with with maps filter fault an
you can do in Cortland as well and
is he here there's I'll all the functions
defined on lists that you will recognize
from the Haskell brilliant most programming languages if you look at the
API
for collections they are very familiar and they all contain
and the same Iran function sometimes
names are slightly different different but in general
and that you'd be very familiar
one nice thing and that got land has
and which C sharp also has our extension that its
so in this case I define an extension that at some
on integer let's look at and the definition for
the some extension at issue here you see and defining
and a love the expression
and this into dots their
indicates that some as an extension method
on ent and it takes parameter logical that other
and its returns and ensure here you see and the
type and then the body of the
I'm extension method just at this
to other show in this case decision scope
and an ranges over
the an receiver and I think this is also
and quite nice its less for boast and the way you define extension medicine C
sharp
very deftly finest static mat did you have to explicitly name the parameter
dish modifier in front and this is
very very concise and then we can just call
Wong dot sum of two
and lectures and when we talked about
a defining data types I compared
haskell at algebra datatypes with glasses
in object-oriented languages and
is word emphasizing that if you look
at the course bonuses if you look at while the similar
haskell als right data type is really
kinda streamlined way to ride
a class hierarchy and with constructors and strong
and better matching is really a streamlined way to do is check
let's see how we would be fine the type of lists
in got lawn and how we can t find
one of the standard triggers it functions over lists
as an extension that'd using better matching
or and est and then you will see
that there's a little bit more syntax then in Haskell or maybe even the same
amount of characters
an but the important thing here is that you can take the concepts from Haskell
algebra dated ives and apply them in an object or language
and vice versa if you have inheritance hierarchy
in an object turned language you can often mobile that also
using haskell algebra datatypes
here's a.m. the type
a blessed so what I do is I D find
abstract class list and this data modifier
is a special modifier an that you can put
on the government class that well and
make sure that a bunch of things are implemented like equals hashCode and so
on
and you know that's rather easy
and then I'd be fine to derived class issue in Haskell these would be on the
right hand side of the algebraic datatype Chanel
is a special case of alleged and college
is a special occasion the list that as a hat
of type T and a tail of typed list of tea
again as you can see if you squint to read or you put on some different
colored glasses
this glass decoration is
isomorphic to the Haskell definition for this
one important difference of course is that and
what then is a strict language so these are not lazy so if you want to do lazy
list
we have to do a little bit more work and I we have to make sure that
an detail remains and evaluated and one way to do that
is by creating a thank for that but thats
topic for a different time now let's look at how we define
and function recursively over
this new list type an so here we define length
and length is defined as an extension method over Leicester you see that
list here has no Matt dense
am but I just define them outside
and here's my better match and I check when this is nel
the result is 0 and I should lined it up properly
so when adl the lest nil
lactis 0 that's exactly like
and has gone and when we have a college
an what i do is it the result is one plus
the length of the day love the list and so instead of
better matching to get at detailed the list
I am calling here the access your
to get detail and since a goblin compiler
and cannot determined that I'm not
and extending this at data class anymore
I have to put here this purist als loss an
to draw an exception but we know that this will never happen
but if if we do this if we don't do this and
we will get an error here and say that arnelle's
clause is required okay and then we can
and create a less DEC looks very similar to Haskell views named arguments here
and recreate the list and that just contains the value 3
and then we calculate is like so let's run this code
and to see if everything I told you
is actually through an
their Eagar
through on it area so and
going from top to bottom this wrong here
is the length of and this last
well that national days only develop you three so
their ego if I compute one place to
history and if I take the numbers 1 to 4
and I'm am double down and then filter out the ones that are less than five I
get the list
and 24 if I print numbers 125 I get the numbers wrong 25
and if I square 42 I get 764
so there we go and programming got land
if you want to learn more about got you can go to the got their website
am and you can jump and immediately
using all the skills you learned and haskell
part of this lecture thank you very much and see you next time