hello everybody welcome back
at the next episode up functional programming 101
and today we're going to talk about
interactive programs I'm but before we start
I want to share a story that I originally heard from Simon Peyton Jones
and I'm in bed Jones and everybody should know
is one of the main drivers behind
has car an also am
one of the founders of the Glasgow Haskell Compiler that
we have been using in this course an
here's the story so in this no box
there is a super powerful a quantum computer
it's extremely powerful it can go to a break every encryption scheme
no known to man it can are sold and the problem it kind of cures cancer
everything the only problem is that
it has no I L so we will never know
whether this quantum computer works
are not the moral of the story is dead
if your program is not able to communicate
with the outside world it might as well
not exist and if you go back to the early days of has gone
it was actually really hard to have a Haskell program
communicate with the outside world where's if you use
and the other programming language it's trivial
the first go to be alright hello world rich Prince hello world
on the standard output whereas in Haskell
that hello world Walsh super complicated
first we had stream i/o then we had continuation based I L and
finally and that you're going to talk about today
we have the I O market and with the i/o Mon at
an
as call has gotten its I O ports
an on the box such that they can easily communicate
an with the outside world alright so
so far we have only reading programs that
take some and food and return a string or are an integer our top all our list
and we have printed that value
an on the out to adopt the
and rebel above the of the GATT interpreter
but what we really want to do is we want to ride
programs like most programs like every
Java program like every see sharp program like
any impaired program that takes
inputs from the actual world does something with it
and produces output which and
in fact what we want is that it produces aren't food
as it's getting new Enbridge
alright so we want to produce our bridge as forgetting
UN which so what this program has to do
it has two interact with the keyboard
and discreet so and if we
look at our program as a function
and some says now it becomes dependent on the keyboard
and b-sides just producing not good
it also will communicate with discrete
the problem
is dead haskell programs are
your functions which means they cannot have side effect
and maybe it's good to go I'll
informally and talk about
what a mathematical function as dramatic a magmatic 'em
law that contractor
a mathematical function is
a function that when you give it this same
argument well always give you the same result
am for example vectorial
when I gave it an
it will return factorial an when I give it and again
it will return factorial and again now look at a common function
that we use in impaired programming for example
redline redline takes no arguments are
and a Mt Dew bottle and readers a string when I call redline again
with the same arguments the same empty to bomb federal return different string
sure redline is not a mathematical function
and the reason is is because it has side effects
so it will return strain
but there's something there that is not apparent in the dock
and that's what's the i/o loan at an
souls the i/o monette gonna expresses
in the type over function that this function
has a side effect Asia interactive programs programs that
indirect with the outside world have side effects
and we need a way to incorporate side effects
in Haskell programs an not
I don't think that side effect are bad am
actually they are and super useful because you know without side effects is
really hard to ride these
interactive programs an but the thing is that you have to make sure
again it's a balance it's a question of taste
how you used shot those side effects it's a little bit like salt or spices
and food
you can overdo it your food is too salty and it doesn't taste well
but if you don't use any salt your program your car
your program your food is also not tasty
a ride George up to you as a developer
to decide how much side effects to use
an but it's never good answer to say
up I'm never going to use side-effect
orange also never good the other extreme to say
I don't care about purity a it is
that's exactly what shows monster if programming
that you can decide where do you side effect
and where to avoid them but if I hear you say
you know
we have to program without any side effects you have not
understood the passions of discourse are gay
so as I said mentioned the solution to the problem is to introduce a new type
and we really don't care how this type is defined
an for for now we'll just issue
its own abstract type I love a
and this type denotes and action
that will perform some side effects and as a result
will return a value of type A
and
maybe a good way to
remembered the difference is that in Haskell so far we've only used
expressions
but we didn't mention statements writers and bird
language there are expressions and there are statements are statements
mostly communicate via side effects and
a.m. in some sense by entered using the i/o Mon at
we are also injured using statements in Haskell
or another way to say that this reintroduce the notion of
actions in Haskell a but these actions themselves
are also expression so they can be compost as you get some concrete
examples
of the i/o monette years I O of jar
adjusted typeof in action that will perform some side effects
and then return a character and then
the and most comparative
of impaired if types that is I O of unit
that is a function that only perform side
the only interesting thing it does is performing side effects
because it has no real result value its readers the empty tubal
am and in a typical imperative language
this would be the type void so if you have a function that returns void
in nashville that becomes function that returns
type I O unit K
and void is not think are you
this Mt Dew ball is nothing special
it's just a two ball and has no components now
that we have this type in again we really don't care how diol more night is
defined
as long as we have operations
defined on them okay so this is like a little bit like
in object-oriented programming we have a class
we have met the John that class in in order to use
a glass you don't need to know how that class is implemented
just you now used the mat and that's the same thing here
with the i/o mod and the first operation that we have
on the eye on it here is get jar in
a typical imperative language GetJar
has died unit arrow
I O of jock unit arrow charge sorry
and as go because we're lazy we don't need the unit arrow
and so instead GetJar is just
something it has a side effect and then
returns a character now if if this would be strict
it would kinda you know rear a immediately return a character right
there
but since has caused lazy in our we don't have to
have this explicit dunk that d first evaluation
let another and dressy thing
in typical imperative languages whenever you see a function that has to be unit
arrow something it means that you're trying to simulate
laziness here's another
an example the function
board jar which rides a character to standard output
it takes a character and edge returns void
it has side effects but it doesn't return and the
usual value and then an
we have just like for parsers we have the
action that immediately returns without doing anything
okay so it takes a value and immediately readers that value
without performing
any for dinner side effects and then
real me using the same kind of sequencing that were used for barger
and again in the exercises you well
see how an did this do notation
is translated in terms of the binds
function and but right now
well just used to do notation because it looks very much like
traditional impaired it got so here's a function
that reach a character region other character but
yeah ignores the result and then region third character
and breeders the barrel does too so if you remember departures from the last
lecture
we had more or less exactly the same example here
except that we used the I'd impartial three times and here
for reading a character from the Standard & Poor's weekend
define a function using GetJar
that reads a string from the Standard & boot as follows: every
she reread the character we call that character axe
am notice that GetJar has died I O of jar
and then X here has type char
so the arrow is very similar to the arrow in generators in list
comprehension show there's an
I O of tea on the right and then there's a
the on the left of the arrow and that's where why
we use the arrow symbol and not shady
ecosystem a bigoted type on this side is different than the dye
on that well if the character that we just read is it
and newline then were done so we return
the empty string otherwise we recursively
read a.m. Hong to read the rest of this character
and then we return the previous character that we're at
concatenated on the string that we're eager
so another gonna recursive an
thing here where we an just
goal this thing itself recursively until we had the new line
and then re return all values here's an example
that takes a string righted do this standard
all if we have the empty string reacted we cannot do anything so we just
return unit immediately if we have a string that starts with the character
axed followed by a string access
be printed their first character and then recursively
we print the rest of the string on the screen an
this guy here readers I O unit so the whole thing readers i IO
unit and everything Fitch now
weekend am the fine
another function that simply combines put string it puts our
to ride the new line behind it so we first print the string
and then reprinted newline the beauty of all this
is that we can mix and match impaired if code or I O
caught red all the functions that we have defined before
and particular functions over less biggest strings are just less
so weekend comparatively read
string from the standard input dan
you wish regular list
functions on that strength and then
and comparatively
windy I Oman at ride this to the standard I
which are large walk turn as example we a
Brent and their strength be reached this
line from the & Boch and then we print
that deadline has so many characters okay
very simple but you see here you combine there's an
island of purity in the sea of impurity
and dishes a perfect perfect perfect way to program
so you have little parts of pure code embed
in a context of impure and when we
I'm run this string length in are nghe I
ever well and print and torturing me type string
and it will tell us that this string has five characters
K and note that this thing is only
evaluated for the side effects it doesn't return
an useful value and we see that
because it has type I'll unit this is the end of Part one
see you in a few minutes when we're continuing to talk about the eye on it
thank you