Press "Enter" to skip to content

Haskell Basics – FunFunFunction #35


good Monday morning I am mvj and you are

watching fun fun function if you are a

regular viewer of the show you can might

notice that this is not my normal voice

ah that is because I have been partying

and screaming for the last three days

which I am actually way too old to do so

today is like what better day to tackle

one of the more complicated programming

languages in the world

let’s start doing your high school

course yeah let’s do that

so then why should we learn Haskell to

be perfectly honest with you I don’t

know entirely why because I don’t know

Haskell so we will be learning it

together on this course functional

programming has been growing wildly in

the last few years my series on

functional programming in JavaScript is

way more popular than I thought it would

be I think the movement really picked up

speed when Facebook launched react I

think that’s when functional programming

really started hitting mainstream

programming recently a lot of people

have started using a react together with

a library called redux which is an

amazing library it allows you to express

your UI logic in a very very functional

manner and makes it really easy to

reason about if you’re not using Redux

and you’re using react you’re basically

living your life wrong you should check

out this tutorial by wes boss on redux

it is amazing so to me Haskell has

always seen like there’s some almost

mythical beast of a programming language

whenever you look at it it looks like

nothing else it’s not a very popular

language however at the same time are

the people that do work with it seem to

really really like it if you look at the

Stack Overflow survey of 2016 it is one

of the most loved programming languages

and what I mean by that is that they

they asked the people what they work

with and the percentage of people

that expressed interest in continuing to

work with that also a lot of very smart

people around me of our advocates of of

Haskell and also a lot of people like

celebrities that I look up to like John

Carmack also speak very highly of

Haskell so it seems to me like it can’t

be too badly spent time to explore

Haskin see what what it’s all about so

as I said I don’t know Haskell so we

need some kind of learning resource here

and for our curriculum I have picked

learn you a Haskell for great good it’s

a free online book you can read it

online for free and you can also buy it

if you want to give money to find people

I have picked this book up primarily

because it has a very funny title and it

also has a son saying holy okay

let’s get started

reading online I’ll learn you a Haskell

for great good all right introduction

about this tutorial so what’s Haskell

what do you need to dive in starting out

ready set go

awesome click that ready set go

alright let’s get started if you’re in

sort of horrible person who doesn’t read

instructions to things and you skipped

it you might want to read no I ah like

the first thing yeah okay so they want

you to install the Haskell compiler you

probably already have that install

because you have tried to learn Haskell

once and then failed ah but if you don’t

uh it you just google Haskell download

and click things until it’s installed

but once you have it you write gay DG

c-h-i and that sounds for great compiler

Haskell are interactive oh it’s great

Haskell compiler interactor right so

they say that you can set the prompt to

gch i ah here I not sure why would you

would

do that because like you are you want to

set that to an emoji I think emoji ah

get list of all emojis copy and paste

let’s find one

what picking an emoji for your hashcode

terminal eyes it’s very easy to get

stuck here as you see now I’m stuck

trying to decide on an emoji oh yeah

these this this is this is possibly the

weirdest emoji in once you have

completed that step we can start

learning Haskell the Haskell is here so

I can type 2.15 1749 times 100 and and

it makes Matt and I can do 5/2 and it

divides it and it’s also yes like

JavaScript in this you can just write

you know math like for 9.99

and you can yeah it calculates it it

also warns about this caveat like if you

you’re using negative numbers like 50

times – what you can’t do that ah that’s

pretty weird also the error here like

this is horrible

uh but either way you need to do

parentheses around – once as a

JavaScript programmer that makes me

kinda happy because people nag about the

pitfalls of JavaScript all the time but

at least JavaScript doesn’t have this

and you can do boolean expressions

like true and false

that’s equals false you know like we’ll

see you can did you do true and false no

true – of course yeah it’s gonna be true

I guess yes and you can also do the or

or operators like yes or false you can

also negate things so not false becomes

true this is like writing this in in

JavaScript and you can infer

qualities of five equals five is true

and five equals four that’s false you

can also check that five is is not equal

to so five is it’s not equal to 4 that

is is true and if you do 5 is not equals

to 5 that is false

double negation um the equivalent on

this in JavaScript would be this right

so what if we try 5 plus hello okay so

it says that no instance of namche are

arising from use of plus in the

expression 5 plus hello in an equation

for it it equals 5 plus hello this is

quite possibly the worst error message

ever devised

but what it’s trying to say is that it

Haskell doesn’t know how to add aa 5 and

hello together because 5 is a 5 is a

number and hello is a string in

JavaScript this had would just have

ended up being a hello and that is

because JavaScript does something called

type coercion it just tries to when it

when it sees something like with this

when JavaScript sees something like this

it just tries to like coerce the two

things into a general type that can be

added together and it squashes them

together which can sometimes be helpful

and sometimes not you know I’m not sure

if I think it’s a good idea or a bad

idea but JavaScript does that ah and

Haskell absolutely no it’s not Haskell

is very picky about the types and it

uses this a lot to try to assist you in

writing correct programs so Plus here in

Haskell it expects both sides to be

numbers I’m not sure if you can can you

plus two strings to get like hello plus

well now you can’t but I could perhaps I

can do this

yes I can so if I hello yeah I can so

equals works on on strings and numbers

but plus only works on numbers however

if I do this hello is not equals of five

it breaks with an error message from the

moon oh but but that is actually because

I’m trying to write JavaScript in

Haskell which does not work no it’s it’s

this one hello and we can try that

compare that to five it’s still a very

hard to understand error message but

what it’s trying to say is that not

equals to five can only compare a things

things of the same type Haskell very

picky about types you can do five plus

four plus one I think yeah that actually

confuses me because it feels a little

bit inconsistent because in my world

this is an int and this is a float uh

and I still think they are in Haskell

but they I guess it did it says that

four can act like an integer but two

five is one that has to adapt I don’t

know it’s confusing you may not have

known it but we’ve been using functions

now all along what okay that means that

plus is actually a function and this is

a function and if I type five five if I

type five times five and that becomes

like this is actually a function and

functions in Haskell that our sandwich

in between like this they are called

in fix functions but most functions that

deal with are non numbers they are

called

fix functions and they are they are more

like what we were used to in JavaScript

so let’s take a look at them

we’ll start by learning the most boring

function Haskell with it which is suck

and suck works like you press right give

it six and then it calls seven and if

you lose sex like a gives you nine what

zero all right

in JavaScript the equivalent syntax

would would just be uh like six but in

Haskell less as you see a castle is

extremely concise it has just much

syntax as you need but no more another

function in Haskell is minimum I mean

like so if I say sex seven it’s going to

give me sex works like I guess there is

a max maybe max yeah so we can combine

this and say min six seven and then add

max six ten thousand and that will give

us ten thousand six so the order

execution of these things are not

obvious but it it function application

which is what has scholars refer to it’s

this thing is when you write a function

and then you add a space and then

arguments that will have precedence over

in fix application like this which means

that it will first execute the min six

seven and then max six ten thousand and

then it will plus them together thus

giving us the right answer however if we

write suck nine times ten that will be

wrong because suck nine will win over a

time stand here like so it will is going

to do suck nine which will be 10 and

then it will multiply that with

and if we want to make sure that this

yeah like that so adding parentheses

works just like in math it just means

that we execute this first before

passing to the function here so this is

one of the things that strikes me with

Haskell if it feels a bit more mathy

than JavaScript does unfortunately this

also means that a lot of examples refer

to math so in in this in the book here

it says that for instance that Dib

functions take two integers and does

integral division between them if you

don’t know what integral division is ah

that’s you’re not alone I don’t know

either

I guess it divides it and throws away

house call this is why nobody uses you

screw that that’s not even what the

author of the book is trying to explain

what they are trying to explain is that

you can apply functions prefix functions

like in fix functions by adding

backticks to them so I can do 92 Dave ah

and AH 10 no that didn’t work

I don’t know backticks backticks that’s

it so I can do that this this statement

here is the same as this one because I

added added backticks here so I guess I

can do this with with Mac’s as well or

something yeah so this is one of the

things that makes haskell look a bit

weird because yeah in in JavaScript uh

all these functions would look something

like something like this right but like

they have up parentheses here and they

have a comma to separate the arguments

but in Haskell it’s much more terse and

looks like that if you see something

like this that’s a bad example normally

like this then that just means that uh

yeah this is evaluated first this has no

real significance in this this thing we

we can just do 92 by the way if I do

suck suck 92 from that give us our 94 no

it won’t I guess I need to add

parentheses yes

so yeah it’s Maddy that’s it for today

you did good we got our feet a little

bit wet with the

the basics of Haskell syntax and next

time we’re gonna look at how to declare

our own functions I’ve linked to the

chapter in learn your Haskell down below

you have just watched an episode of fun

fun function new episodes every Monday

morning make sure that you visit the

channel and subscribe so that you don’t

miss out on the next episode I mmm PJ until next Monday morning stay curious

Please follow and like us: