Press "Enter" to skip to content

Haskell lists – FunFunFunction #39


hello hi mmm PJ stars you are watching

fun fun punch in this series we are

learning Haskell together together I

don’t know Haskell so we are learning it

through a free ebook called learn you a

Haskell there’s a link to it in the

description below in case you randomly

stumble over this video and you’re

confused you can view the entire series

from the beginning by clicking the I in

the upper corner the topic for today is

lists you can find a link to these

specific chapter in the book that this

episode is about in the description

below so let’s jump into it I have how

did you do this

gah okay cool we’re going to load lists

not HS which is the file we’re working

in here looking at the chapter lists

reminds me a lot of erase in JavaScript

but at the same time it also feels like

lists have a much more important role in

Haskell than Ray’s play in JavaScript so

this is how you define a list left lost

numbers and one two three six BAM and

then you go numbers at this point the

book introduces late but I don’t feel

like it explains exactly what it’s for

the book says that we use left to define

a name in the GHC I you know this if

you’ve forgotten what names are you can

click there for a refresher they’re kind

of like constants or or symbols kind of

however this confuses me because I can

kind of I can just lost numbers two

equals two three four seven and then I

can do less numbers one less numbers two

and they both work I just don’t

I don’t understand why it wants me why

the book talks about let so if you know

that please comment down below so that

you can educate me and your fellow

viewers one way that lists in Haskell or

different from arrays is that erasing

JavaScript allows you to mix types but

they’re not allowed in in Haskell so if

has goal was JavaScript I would be

allowed to do like four eight and hello

and this would be a valid array in

JavaScript but it’s not in Haskell so

you get an error message sort of like no

instance for num char arising from the

literal four well I need to have only

numbers in it I can have strings in in

lists waffle cat and that will work

they just whoops but they just have to

be the same type in the array when I

said that I felt like lists are more

fundamental to haskell than arrays or

JavaScript one of the things that made

me feel like that was that strings are

actually lists in JavaScript strings are

like a separate thing from a race but in

Haskell when I write like this cat that

is actually just syntactic sugar for

writing this cat BAM OOP so in Haskell a

string is not a string it’s actually

just a list of characters strings or

lists so a common task is to put two

lists together and because strings are

lists we can use the same functionality

to concatenate strings as well so we do

hello waffle cat and we use the plus

plus operator to do this helpful cut so

in the last episode I was come

used about the fact that you couldn’t

just plus strings together that in high

school like you can in most other

programming languages that is in error

but now that I know that strings or

lists and you use a special operator to

concatenate lists kind of makes a bit

more sense the book warns here about a

little performance caveat when when

using the plus plus operator or long

strings so let’s imagine that this was a

very very long string for some reason

and we just wanted to add a you know an

a very short string at the end

internally haskell will hear how to walk

through the entire list character by

character before appending the the the

string on the right this isn’t much of a

problem when you’re just dealing with it

like do small strings then you don’t

need to bother but imagine like list

like it’s a million item small then that

will impact the performance of your

application however if you have a list

that is oh then you can use something

called the cons operator which is colla

and you can add things at the beginning

like this this however is instantaneous

and doesn’t have the same performance

characteristics notice then that the

concentrator it takes a single number

doesn’t take a list the plus plus

operator needs a needs the thing to be a

list so if we want to do the same thing

with a with a plus plus greater nine

here that would fail we would have to

add lists on both sides and because of

this performance aspect the the course

operator is actually used internally by

haskell so when i do one two three this

that is just syntactic sugar internally

haskell will write who will rewrite that

as

one cons to the cons three cons and the

list Punk and that gives us a list with

one two three if you want to get an

element out of a list at a specific

index you can use the exclamation mark

exclamation mark operator so if I do

like to hear I’m gonna get e because

that is index 0 index 1 index 2 here and

remember that these are just lists so I

could just do 1 2 3 and I could do

exclamation mark exclamation mark index

2 and that would give me 3 because index

0 index 1 index 2 3 here and that gives

us 3 just like arrays in JavaScript

lists in Haskell can contain other lists

so we can do 1 2 3 4 and that can be

inside another list did it that works

and we can’t get out the the second list

here by looking there and we can of

course add other lists to our list of

lists by doing this like five six boom

oh no that won’t work because doing that

would be like adding this and that’s not

allowed because this is this this list

here this is a list of lists with

numbers in them and adding numbers

directly to them would be mixing types

so what is allowed however is doing

doing five six and a list so now we add

a list of Lists with numbers in them

to another list that is also a list of

lists with numbers in them and that will

work hopefully what I need to do plus

plus yes and I could also do this with

the concentrator I guess but here I

could probably actually win this one

yes that would work lists can be

compared if the stuff that they contain

can be compared for instance we can

compare like five is five larger than

six no it’s not we can compare numbers

therefore we can compare lists of

numbers so we can see if for instance

like one three we can see if that is

bigger than zero

ah to which it is in a little bit we’re

going to take a look at list functions

Oh so with functions so let’s say that

we have a list like 5 4 3 2 1 we can

call for instance head on it that gives

us 5 because this is the first item is a

5 think of the list like a monster I did

tail and that will give us this there’s

a tail this is the head tail so you

asked me how do you get the but you do

that by saying list that will give us 1

because 1 is the last item then there is

the inverse of the tail function which

is in it so you just go in it and what

that will give you everything but the

but if you try to do head on an empty

list you’ll get an error the type system

of Haskell allows us to catch a lot of

errors at compile time but this is one

of the errors that we can’t and in

Haskell we want to avoid runtime errors

as much as possible so it’s good

practice to make sure that we’re not

passing any empty race to functions such

as head to do that we have functions

like length that’s 0 you know like 1 2

that will give us 2 there is a function

that actually checks if an array is

empty and that is funky enough called

null so if it’s not empty it’s normal

it’s going to be false and if it’s empty

it’s going to be true we have other

functions like of like a reverse which

not surprisingly will reverse the list

there is a function that is called take

and take will grab as many items from

the beginning

of the array that you specify so I say

take three and that will take the first

three items from the array not a right

list still living in JavaScript land and

you’re still straight if I take two it

will take the first two items if we take

zero it will not give us an error it

will give us an empty list to take more

than exists okay so it it still works if

I say like a thousand it will just give

us as much as is available this is also

kind of inverse of take which is drop so

it just drops the first two items on the

array and returns the rest there are

functions like minimum well mini mini

mum which gives us the smallest number

and of course there is maximum which I

can’t spell these things maxi but

maximum which gives us the largest

number and there is things like some

which summarizes all the numbers and

there is things like product which will

multiply all the numbers like 5 times 4

times 3 times 2 times 1 is 120 and there

is a function called LM which checks if

an element is part of a given list so

let’s say that we have 5 4 3 2 1 and

check it element 6 is part of that no

it’s not

however element 5 is or 3 some people

find this a bit hard to read so LM is

often called as an infix function and

lamb and then true if you’ve forgotten

about error in fix functions you can

click here

for rehearsal but they are basically

functions that you place in between the

arguments any function can be called as

an infix function but there are no the

normal infix functions or plus like this

is an infix function no my battery died

on my keyboard and it finally I want to

point out something that the book

doesn’t talk about but is a very clear

difference from JavaScript because

notice that everything here are like the

functions are separate from the the the

list in Haskell we have length that is I

just a function that takes a list but in

JavaScript length is a property on the

on the array object and this shows how

Haskell is very much not an

object-oriented language it is function

oriented it’s functional in JavaScript

and other languages that has a lot of

object orientation States and functions

are intermingled so length and and and

the array here they are like combined

together

however in Haskell they are cleanly

separated same thing with would take for

instance the JavaScript equivalent would

be probably slice which would be like

0-2 and that’s it for today you have

watched an episode of fun fun function I

released this every Monday morning oh

wait hundred Greenwich Mean Time

if you found this interesting you might

want to check out the channel quick

frets below to see if it’s something

that you would consider subscribing to I

am mpj until next Monday morning thank you

Please follow and like us: