the same problem of summing up from 1 to
10 is actually done by what would appear
to be magic by simply going some 110
done it's not quite as easy as that one
of the questions that I get asked when I
introduce myself as a computer scientist
is what exactly it is I do and the
easiest answer for that is programming
there are different styles and a lot of
people might not be aware of them so
today I'm just going to contrast two of
the most popular ones namely imperative
programming and functional programming
adding the numbers from 1 to 10 in two
languages Java and Haskell so we'll
start with Java we begin by introducing
a variable total and integer and saying
well that's 0 to start with because we
haven't added anything up yet and that's
comparatively easy and the actual meat
of the loop is just done by opening up a
for loop where we say for some number
int I equals 0
and I less than or equal to 10 then I
plus plus and what that means here is
we've got this little tracking variable
I which we initialize to 0 we're going
to be repeating this loop as long as
it's less than or equal to 10 and every
time we repeat the loop we're going to
increase I by 1 we're adding numbers 1
to 10 so the obvious thing to do is to
say we'll change the variable total to
the result of total plus I what we have
here is we've got an appearance of the
equals sign some people would look at
this and go right this is clear the
mathematical definition of equals which
means the left-hand is equal to the
right-hand side that's not the case in
Java and other seed like languages what
this single equals actually means is an
assignment we're saying that total is
now equal to the previous value of total
and we've edited it in some way in this
way we've add 1 to it if we were to have
a double equals then we would be
checking for equality in which case this
total equals total plus what I would be
false in all cases except for when I was
0 so this kind of statement that
involves variables such as total is
characteristic of imperative languages
where the
main method of computation is assignment
of values to variables that's going to
say we start with total at zero they say
of I is zero total equals zero plus zero
zero then we go back we increase I by
one so now we say okay total is zero
plus one which is one we go back is now
two total is one plus two it's now three
and we repeat once we get to the ends
then we're done if we want to call total
then it would just be the result of
going one plus two plus three plus
doubled and eventually at the end of it
we'll have total if we were to do a
calling rate is 55 because that's the
sum from one step this is a
characteristic of imperative programming
in that we're referring to variables
that are defined globally this is a
simple example of adding a couple of
numbers up in an imperative language
namely this is done in Java what I'd
like to do now having just spoken about
the mathematical definition of equals
move on to a nother language in which to
do this summing from one to ten namely
Haskell which is a functional
programming language so the same problem
of summing up from one to ten is
actually done by what would appear to be
magic by simply going some 110 done it's
not quite as easy as that there is a
library function in the background
that's explaining how some actually
works the first thing that we need to
know about the sum function is what its
type is so we would say that the
function sum is the function that takes
a list of integers that's a syntax for
lists and it returns a single integer
namely if we take a list from 1 to 10
we're going to return the sum of them
and what we're going to do now is we're
going to define this function in terms
of pattern matching a function can have
multiple definitions associated with the
value that's given to it what pattern
matching is is a way of deciding which
definition of a function to apply in a
given case depending on the structure of
the input so let's say that we are
looking at a function
that tastes lists then that function
could be looking at an empty list
it could be looking at a list with one
element in it or it could be looking at
one with more than that a simpler
example perhaps is a function that's
defined over the billions so a billion
value you can either be true or false
and a function that's defined over there
the obvious one would be not so not true
is false not false is true and if we're
using pattern matching I'm going to take
my value and say is it true if the value
that I've got is true then I use the
function definition I'm going to false
if the value that I've got is false
it'll say I'll look at this first line
and well if I got a true no I don't I'll
go down to the next one and I'll say ok
well I've got a match there when you're
looking at a function your input you
will try and match from the top say is
that the one I need no right look at the
next one is that the one I need
no is the next one and it may be the
case that you don't have all of the
patterns necessary to fully define your
function in which case haskell will give
you an error it'll say non exhaustive
patterns so if we have this sum of a
single number this is a list of one
element we say that the result of the
sum of one number is just that one
number and if we have a list of more
than one number then we would write it
like this
sum n ends this is just shorthand for
saying that the element that's at the
front of the list we call n all the
other elements are called ends and we
would just define that as n plus some
ends so this function sum is defined
recursively that is to say it's defined
in terms of itself what I might like to
do for some people that aren't familiar
with functional programming is go
through a very quick example of how the
sum function works some of 4 5 - so we
can refer to this function here and
throughout and see how this is
equivalent to the Java representation
that we've got over here so what pattern
matching involves we look down and say
are we looking at a single list no we're
not so we can move down to the next
pattern and we say ok in this instance
this 4 is the N and this 5 - is the ends
so this is defined to be 4
plus the sum of five and two grand next
step we're going again
sum is defined in terms of itself we
haven't gotten down to base value yet so
we're going to have to carry on so we do
so by saying okay so what do you mean by
base fun
okay so functional programming the main
method of computation for that is
applying arguments to functions as
compared with Java or it's assigning
values to variables by here when I say
we've reached a value I mean that we've
evaluated a singleton and returned n so
to carry on with this evaluation here
what I want to do is to say okay that's
some five plus sum of two because in
this instance here this was the N and
this was the ends and now I carry on and
I say oh well if I'm going to be correct
I'm going to bracket that and say four
plus five plus and now I can look and
say pattern matching is stuff from the
top have I got a singleton list yes I do
so what am I going to return just that
number two so now I can start boiling it
down say oh that's four plus seven
equals eleven so what I've gone through
here is just a quick example of how a
functional programmer would take the
approach to some function defining what
the sum actually does as opposed to
looking down and doing it in terms of
variables saying we'll track this and
add it to this and eventually the result
will fall out of it it's it's probably
worth saying though that this isn't how
sum is defined in the Haskell library
it's actually defined as some fold l
plus 0 but that is dark art that I don't
quite want to get into it this video say
one particularly important thing to take
away here given that I've been
contrasting these two is that in this
example here where I've been summing 1
to 10 in Java this is revolved somewhat
around this variable total which has
been evolving as the for loop progresses
I've been updating this total variable
and increasing its
to reflect a new value until I get to
the end of the for loop so this is
characteristic of imperative programming
languages the fact that I have a
external global variable that I can
update it will whereas in functional
programming in Haskell there is no state
externally I'm not calling anything from
outside this sum function this sum from
1 to 10 is defined entirely in terms of
itself there is no reference to a total
or something that's not explicitly
reachable from inside this function body
and that's because functional
programming languages or purely
functional programming languages don't
allow for side-effects and a particular
example of a side-effect as a mutable
states whereby mutable I mean it can
mutate you can update it so it can
change from one value to another as a
program progresses it's worth saying
that these are both high level instances
of languages and particularly simple
instances of fragments from these high
level languages these do look quite
different in the way that they approach
things but they will be boiled down to
machine code and very similar machine go
to that given that it does something
quite simple and in effectively the same
way something 1 to 10 is not something
that can be done very in very
complicated ways by different compilers
what if the universe is just a computer
simulation how would we know what sort
of things we expect to see could we ever
tell could we ever be able to work out
that work you know living in part of a
simulation or we're just a bit pivot and
putting all elements less on it on one
side and then a great start on the other
side and then it there does that get any
to sum each side so you can see it's
kind of it's done all the left-hand side
and now I start doing the right-hand
side so there's a pivot it does all the
left-hand side it's another P that keeps
doing this this animal till it's sorted