welcome everybody to
another episode of functional programming one along
and today we're going to talk about
declaring types and classes ourselves up to now
we have only in the and lecture on parsers
we have defined dives ourselves an
but for the rest we gotta have issue data types are there for example we have
issue
that the dye Bliss Texas then we have assumed that doubles exists
but of course just like we can define new function ask our
we also want to define new types
and well we're going to do in
dis lectures we're going to show an
how to declare new types and how to dig their new type gloss ish
but also I'm going to compare and contrast this
with typical object-oriented programming
am how these things work there and what you will see
is that although this index is a little bit different a lot of the ideas are
very common
and this is a general thing that I like to do instead of emphasizing the
differences
I think it's often much more productive to emphasize
commonalities okay and and and
maybe you know you should try dish do it
alas programmers we often gotta go well but no
it's much more productive to say yes and
and emphasize am you now
but this common am and so I'm going to try to do that here
and and please take that as a metal ass and
for this lecture alright
am and Haskell an weekend
the final alias for an existing type
an by saying type strain
equals another time are
type you now new name equals another type
and in this case we are defining string
as an ailish for a list of characters
and this is it and an idea
that we have used throughout all the lectures right every time we saw
straying
we treated it as a list of characters but n-type three of them wrote strength
biggest
that's what we want to do convey but when we were going against range
we want to see them as a list of characters and
and here this type declaration achieved that
this is just another name fordice died
good and
what we often do is reused this just like which train
reused type aliases to convey the meaning
our intent of what we want to do for example instead of having a pair of
enter chess
we can see a while we declared this as
diaper position and then instead of
in our writing in Tacoma and everywhere
we can ride position showed the Orijen thats
at cornered 00 and then we want to go if they are
move a position to the laughter is a function that takes a position
every there's a new position and it takes a bear
XY and it subtracts Guam from
X alright and now by looking at this dive
you get a better idea of what it's doing
and the fact that we're using bears to represent positions
is really just an implementation detail another example here
is a parametrized type
alias so here we define the type of bearish
of Taipei
as bears of Taipei
see so the type of bears
a value of type A are bears values
of Taipei and now we can define functions
over debt type for example we can take a pair of integers
and return an integer by multiplying the two
or we can take a single value
and copy that into a pair
and there's the function got be which takes annex
and returns a tuple that contains another copy of Mac
type decorations
of course can be nested saw it
if we define position as a pair of integers
then weekend define another type Shin am
to me position to position so
a transformation other position as a function that takes one position
an takes every turn another position so in this case here
we could have defined this function here to have typed
trench now there's one important thing here is that these
and type synonyms I really just synonyms they're just abbreviations
and which means that they cannot be recursive so in this case
where you define type3 to be
a pair of an event analyst of threes
this is not a lot and when
you can t find and we will see that later its
no problem to define recursive types in Haskell
but they always have to go be a normal type
an and this is a restriction that
many languages have if you have an object-oriented
a language typically it has a
a nominal type system and that makes it easier to check
whether to died sorry call our mom died this is sharp dive bar of another
and if you have purely structural dives that the gums
a little bit more difficult so it's them that this
typically the reason for that an but if you're interested
programming language in type systems the field of structural verses nominal types
is it very interesting
a field and you know you can spend years researching that am reading papers on
that and
you know get a PhD in that area right I said this when we want to
make a recursive dive we have to go through a nominal typed
and the way we define nominal types in Haskell
is the I data declarations
so here we're defining a new type named bull
and it has two constructors
through and false K
and these constructors are don't don't take argument
they are at the same time values if you
no skyline you will see that skyline
case classes are very similar to algebra
datatypes an but if you did
you know even if you don't know scholar but say you know a job our C sharp
one way to look at these data decorations is as follows:
this is an abstract base class ball and then
it dives here the under right hand side
are the subject show falls extensible
and through extensible and since bullets elvis abstract you cannot create an in
search of bull
you can only create instances of false and through
but both have died bull and Haskell
and true and false don't have their own types they always have died bull
so they that even simpler than having an abstract base class
and two iraq types as I mentioned
the their values on the right hand side there to run for a false
are typically gold constructors an
and yeah with explanation I gave
with the abstract I band
subtypes that make sense they correspond
two constructors in an object-oriented language
and and Haskell died and constructor names
must begin with an uppercase letter SL
functions and died very bones began with a lower case letter
and dives and construct their names must begin
million a British latter and these algebra
datatypes another way to look at them are as context-free grammars
where the thing on the left hand side is known on terminal
and then their right hand side can an contain
and terminals which are the constructors that themselves can recursively
contain other types like a grammar contain
and recursively on the right hand side unknown terminals as well
Heerden an example
a slightly more interesting as you brag datatype
and its like a boolean thing red
three values three valuable ensure we have yes
no and don't know so this is maybe like sequel
where this is like you now know so is it revalued
logic and now weekend use this thing
Tuesday I want to have a list of answers yes no and no
and now I'm going to define another function that flips the answers i buy
this is like the generalization of negation
and if I have yes and I let that becomes no
if I have no and I let that it becomes yes
and if I had if I don't know and I flatbed
I still don't know writer if you're familiar with chic wall
dish will be very similar where'd
unknown plays the role not
here's the and standards oooh tombaugh love circles and rectangles and shapes
an ascot looks as follows: we haven't
abstract base class shape and Duceppe type circle
and rectangle both of them are shapes
and in Haskell circle and rectangle don't have their own types
so when you create a sure goal using the circle constructor
you get the shape and if you grade the rectangle using the rectangle
constructor
you also get a shape alright that's the main difference between
algebra david ives in Haskell an
the way you'd typically rides inheritance hierarchies
in or language now we can
creates use this constructor to create
particular shape a square so
we take a value and for
the a first gordon et there and weaker in the shape
I just duplicating that value here and we can
computer the Ariana Ave shape
and that will return afloat as a year you see that Ariana
is defined on shape okay and then we do
a better match here which is very much like doing
a dynamic dispatch so if
the shape happens to be a sure call then
II take by times are square
if the shape is a rectangle
I just multiply sites okay
but so better matching got of course bonds
to virtual dispatch and when you think
of algebra data dives as object hierarchies
now
when we look at these an construct arse
and that we define in here
they implicitly defined constructor
functions that they could float and return to shape
and
second function that takes two floats
and returns shape and these functions are Gold Circle
and rectangle so if you look at it I believe you ask
goal on TV of circle haskell the EU
float arrow shape and if you ask
what is a diaper rectangle it will tell you it's a function that takes two
floats
and returns shape okay
so again the anthology red and objects
is still intact an you define
your ship dives and really what you get for free
is constructor to construct the stocks we mentioned
maybe type a couple of times and maybe dive is like a list
that's either empty or has a single value and
usually convenient to just use list but
if you really want to use the maybe died maybe
here in F sharp person where are ml person where maybe is used a lot
well you just defined in the large iraq datatype
may be okay is either not think are just
and a and all we can define am
lawler functions and that people are kinda in a proud of
because we can take a list and we can take the heads
but if the list is empty the return not think
and if the list is not empty we take the hat
Mb return just I'm not a big fan
up this kind of definition of had because we're not still stuck with this
maybe
so you really have now reduced to list
to a singleton list budget yet not really gotten the hat out and in order
to gonna be a go for men may be a
a2a you will still run in the same difficulties
as you had when you and from a list from an empty list
to a WYD proper head okay
so that's why I am I typically don't care about
this maybe die bitch its it stands in the way
and it doesn't help you because she at some point you have to get rid of it
and you
alright that was the end of Part wrong
let's take a short break and I'll see you shown
for part two