Haskell - Baby's first functions - FunFunFunction #36
Skills:
Algorithm Basics80%
Key Takeaways
Defines and combines functions in Haskell, including if-then-else statements, based on the book 'Learn you a Haskell'
Full Transcript
Good Monday morning. I am MPJ and this is fun fun function. This is a series where we learn HA hasll together. I don't know Haskell. We are learning it from the book learn you a Haskell. There's a link to it in the description below. If you just stumbled across this video, uh you might want to watch the episodes from the beginning by clicking there. In this episode, we will be looking at a segment of the book called Babies First Functions. Link in the episode description. This segment of the Learn You a Haskell book talks about defining functions. And this is how you do it. Double me x = x + x. Now we have a function and I can call double me five and that will give us 10. We're learning. Somebody in the comments complained about me using a lot of paper in my episodes. So, in honor of that person, I bought these huge ass papers and these huge pens. Anyway, you have the function name here and this is the parameter and this is the function body. I'm going to copy this uh and bam. I'm I just saved that as a file called baby hs. And according to the book, this is how you load it. So it's like colon L and then baby. We are not going to do that because that's insane. I want to write out load. Okay. Compiling main baby. HS interpreted. Uh and okay modules loaded main. What? I didn't why main? I I didn't define main anywhere. Uh maybe that's some default mod. If you know that, write down in the comments. Either way, I think we can now call the the function. Yes. And I get IntelliSense with with tab. That's pretty cool. So I can call double me sex and I get 12. Does that work with uh with with with with floatingoint numbers? Yes, it does. So next up, the book wants us to do a function called double s. And it's going to take two arguments x and y equals x * 2 uh + y * 2. And the book does that. But I won't spaces. Uh I will load baby again. And we'll call double us. Uh three seven. That's 20 maybe. Okay. Yeah, we'll trust. Not unsurprisingly, you can call your own functions inside of of other functions. So I can go uh uh uh double mex and double soil double double I can't type double knee and I can remove that. So you see that I'm now referring here and here. So this this is the same thing. I'm blowing your mind here. But joking aside, this is a pretty good example of a pattern that you will be seeing used a lot in HASLL. Combining functions, more specifically making basic functions that are obviously correct and combining them into more complex functions. And we do this in order to avoid repetition. This is not a new concept. Old programming languages have it. But because H hasll is so crazy turret, just look at this. Uh it allows becomes sort of supercharged in Haskell. Just to give you an idea about how little syntax it requires in Haskell to define a function, I'm just going to go and implement this quickly in Java. So I'm just going to write a public uh class because everything in in Java needs to be a class. Uh my math utils uh la. And I also like public public static void. No, it's going to be an int uh my double me and it's going to take an int and it's going to take x. It's going to be where uh and it's going to return x + x. So look at this. This is I'm going to pull this up for a bit so you see it. uh this is 88 characters and this is say what 18 characters. So Java syntax is 480% of Haskell syntax and remember that both of these are statically typed languages. In JavaScript, this would be uh double double mex uh x + x and you would have to say const as well or possibly bar to be nice. If I had said that, yeah, these like look how much shorter JavaScript is than Java. You could have said that uh there's all kinds of typing information here that adds uh add safety. But in the Haskell case, Haskell actually has a strong static type system just like Java does. Uh the difference is that Haskell infers these things for us. So we don't have to declare them uh explicitly like we have to do in Java. And yes, Yava people, I know that this comparison has several problems, but I do it to make the point that Haskell has a very low function overhead. What I mean by that is that it is a lot cheaper to create a function in Haskell compared to most other languages. Related to this, I want to mention another episode I made on arrow functions in JavaScript because I think that there is a lot of Haskell in the arrow functions in JavaScript. You can check out that episode by clicking there. But we're doing Haskell here. So, let's delete this Java and JavaScript. And we're back to Haskell. By the way, it doesn't matter which order you define these functions. So, I can take this here and I can move it up here and I can uh load baby again and I can call double us uh seven. What does this error even mean? Interactive 91 error. No instance for show a z arrow a z arising from the use of print. Maybe you haven't applied a function to enough arguments. Oh, wait a minute. Wait a minute. Oh, it it was actually helpful. Uh 7 8 30. I just called it with seven here and I needed to call it with seven and eight because well it has has two arguments. But the point that I was going to make was that uh even though we define double us here, we can refer to double me uh in this function here. Even though that is defined on line two. Now we're going to take a look at if then else. So we're going to write a function called double small number which is going to if the number that we give it is uh smaller than 100, it's going to double it. Very useful. So it's going to take the uh number the the argument x the number x and uh if if x is uh uh over 100 it's just going to then just going to return x else it's going to be x * 2. Let's load that uh load baby. And we're going to call double small number. And we are going to say uh 101. It's going to return 101 because that's not a small number. But if we call double small number with say seven, it's going to double it. So you've seen an if else before. But a subtle thing in HA hasll is that else is mandatory. It simply must return something. There's no null. In JavaScript for instance, you could just omit the else. Uh so I could do a function double small number in JavaScript. And I will like if the number x is below 100, I could return uh return return x* 2. And if it wasn't uh two here, it would just go to the next line and it would uh return undefined, which might be a nasty surprise for the person using the function double number. So if JavaScript was HA hasll, the compiler would have forced you to do something like this. I'm going to delete this JavaScript code. By the way, you could write this on one line just to show you that that is uh that is perfectly possible small number 74 still works, but dividing into multiple lines is is it's I don't know. I think it's pretty nice. The book talks about the fact that this is an expression. So what's an expression? Uh let's see expression examples. So an expression is simply a piece of code that returns a value. So five is an expression that returns a value. 4 + 8 that's an expression. X + Y that's also an expression. And now this is also an expression because this will always return a value. That's why you need the else because if it didn't have the else, this would not really be an expression. It would not always return a value. This is also where parents come in. Let's say that we wanted to add one to the value that we doubled. Let's load that. Oops, I have a parse error because yeah, this this is not valid. task. Let's delete all that. Let's load it again. Uh, all right. And I call double small number six. That's 13. It doubles it and then it adds one. But if I call double small number [Music] 100, we expect this to be 102 or we want this to be 102. Uh but it's still 101 because it's actually evaluating like this, right? Um if x is 100, it's going to uh return x or else it's going to return x * 2 + 1. So if you wanted to add the one either way, we need to add parn here. And that is what makes this a single expression. We use the parns to denote that this is an expression. In the book, they suggest adding this to the function name. This is a bit funky to me. Uh I let just going to load that. And then you call double small number and and and this is not a special character in Haskell. It's a completely valid uh uh thing to have in a function name. But the way I understand it is that you tend to add these when you have a function uh that is uh slightly uh modified version of something. So in this case I guess that we add it because you know it's it's doubling the small number kind of almost isish. I don't quite understand the need for this convention yet. Uh if you have an insight on this, please comment down below. But either way, uh these are like you could do Conan O'Brien and it Sami O'Brien. And that should work. phone right it should be noted here that we cannot do this I think so if I do know that I get some weird error data constructor conan O'Brien not in scope uh so functions uh have to uh cannot begin with a capital letter the book says that it will talk about why later and I guess that it will have to do with these data constructors maybe and we should also look at the fact that this doesn't take any function parameters. So this is a a function that is just this expression. This is a function that will always return this string. It's a me colon Brian to the JavaScript part of my brain. This looks a lot like a variable or I guess a a constant because I it's not possible to redefine it. So I assume that in HA hasll maybe you don't use constants at all. You just use this functions that uh are defined to values in HASLL. Functions like this, functions that uh don't take any parameters, they are referred to as definitions or sometimes you just call them names. The Ruby part of my brain, it kind of draws a parallel to symbols. Not sure if that's correct. That is the end of us reading uh or learning the uh baby's first functions segment of the book Learn You a Haskell. If you want to try this on your own, there is a link in the description to this exact segment of the Learn You Haskell book. Next time we will be looking at lists. Do not miss that. You have just watched an episode of Fun Fun Function. I release these every Monday morning. If you like this episode, you should click the uh channel below and check out if there are other episodes uh that you you like and and there will be and so you should subscribe. I am MPJ. Until next Monday morning, stay tuned.
Original Description
💖 Support the show by becoming a Patreon
https://www.patreon.com/funfunfunction
Function basics in Haskell, based on the book "Learn you a Haskell". Haskell is a statically typed, purely functional programming language that is really good to know if you want to learn functional programming.
►00:17 Learn you a Haskell Book
►03:19 Combining functions in Haskell
►07:14 If then else in Haskell
►09:33 Expressions in Haskell
►13:44 Definitions in Haskell
I'm also active on:
► Twitter https://twitter.com/mpjme
► Medium https://medium.com/@mpjme
► Quora https://www.quora.com/profile/Mattias-Petter-Johansson
Resources:
► Chapter from the book that the video is a about
http://learnyouahaskell.com/starting-out#babys-first-functions
►Watch the Haskell series from the start
https://www.youtube.com/watch?v=j5a9l1Td2Lo&index=1&list=PL0zVEGEvSaeG37ziHqYumYgvJDBn1Wsap
► Arrow functions in JavaScript
https://www.youtube.com/watch?v=6sQDTgOqh-I&list=PL0zVEGEvSaeHJppaRLrqjeTPnCH6vw-sm&index=2
► Learn you a Haskell (free online book)
http://learnyouahaskell.com/
► Functional Programming in JavaScript (another series of mine)
https://www.youtube.com/playlist?list=PL0zVEGEvSaeEd9hlmCXrk5yUyqUag-n84
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Fun Fun Function · Fun Fun Function · 43 of 60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
▶
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Higher-order functions - Part 1 of Functional Programming in JavaScript
Fun Fun Function
Map - Part 2 of Functional Programming in JavaScript
Fun Fun Function
Reduce basics - Part 3 of Functional Programming in JavaScript
Fun Fun Function
Destructuring: What, Why and How - Part 1 of ES6 JavaScript Features
Fun Fun Function
Reduce Advanced - Part 4 of Functional Programming in JavaScript
Fun Fun Function
Closures - Part 5 of Functional Programming in JavaScript
Fun Fun Function
Too many tools and frameworks!
Fun Fun Function
Currying - Part 6 of Functional Programming in JavaScript
Fun Fun Function
Recursion - Part 7 of Functional Programming in JavaScript
Fun Fun Function
Promises - Part 8 of Functional Programming in JavaScript
Fun Fun Function
Staying relevant as a programmer
Fun Fun Function
Factory Functions in JavaScript
Fun Fun Function
Composition over Inheritance
Fun Fun Function
Software needs to be better - FunFunFunction #1
Fun Fun Function
Unit testing: How to get your team started - FunFunFunction #2
Fun Fun Function
Straight-line code over functions - FunFunFunction #3
Fun Fun Function
Clojure - FunFunFunction #5
Fun Fun Function
The growth stages of a programmer - FunFunFunction #6
Fun Fun Function
5 tips to quickly understand a new code base - FunFunFunction #7
Fun Fun Function
Semicolons cannot save you! - FunFunFunction #9
Fun Fun Function
Functors - FunFunFunction #10
Fun Fun Function
Functors: I was WRONG! - FunFunFunction #11
Fun Fun Function
Questions and Answers - FunFunFunction #12
Fun Fun Function
Streams - FunFunFunction #13
Fun Fun Function
Prototypes in JavaScript - FunFunFunction #16
Fun Fun Function
Fast or Flexible? - FunFunFunction #17
Fun Fun Function
Coders are herd animals - FunFunFunction #18
Fun Fun Function
Weekend Kubernetes Shenanigans - FunFunFunction #19
Fun Fun Function
Monad - FunFunFunction #21
Fun Fun Function
Moar Weekend Shenanigans - FunFunFunction #23
Fun Fun Function
Questions and Answers - FunFunFunction #24
Fun Fun Function
Losing motivation - FunFunFunction #25
Fun Fun Function
LONGEST KUBERNETES SHENANIGANS! - FunFunFunction #26
Fun Fun Function
Fast code is NOT important - FunFunFunction #27
Fun Fun Function
Pair Programming a Facebook Messenger Bot - FunFunFunction #28
Fun Fun Function
Writing unit tests for personal projects? - FunFunFunction #29
Fun Fun Function
Let's Code a Pomodoro Button - FunFunFunction #30
Fun Fun Function
What editor do you use? - FunFunFunction #31
Fun Fun Function
Arrow functions in JavaScript - What, Why and How - FunFunFunction #32
Fun Fun Function
Is Programming Art? - MPJ's Musings - FunFunFunction #33
Fun Fun Function
Generators in JavaScript - What, Why and How - FunFunFunction #34
Fun Fun Function
Haskell Basics - FunFunFunction #35
Fun Fun Function
Haskell - Baby's first functions - FunFunFunction #36
Fun Fun Function
Is Big O relevant to you? - Q&A Part 1 - FunFunFunction #37
Fun Fun Function
How much are you allowed to Google? - Q&A Part 2 - FunFunFunction #38
Fun Fun Function
Haskell lists - FunFunFunction #39
Fun Fun Function
var, let and const - What, why and how - ES6 JavaScript Features
Fun Fun Function
Why are some programming languages popular? - MPJ's Musings - FunFunFunction #41
Fun Fun Function
Does a developer need to be nice? - MPJ's Musings - FunFunFunction #42
Fun Fun Function
bind and this - Object Creation in JavaScript P1 - FunFunFunction #43
Fun Fun Function
Examples of this and bind - Object Creation in JavaScript P2 - FunFunFunction #44
Fun Fun Function
Prototype basics - Object Creation in JavaScript P3 - FunFunFunction #46
Fun Fun Function
Separation of concerns RANT - MPJ's Musings - FunFunFunction #47
Fun Fun Function
Cellular Automata - Pair Programming - FunFunFunction #49
Fun Fun Function
The 'new' keyword - Object Creation in JavaScript P4 - FunFunFunction #50
Fun Fun Function
__proto__ vs prototype - Object Creation in JavaScript P5 - FunFunFunction #52
Fun Fun Function
Unity game pair programming - Let's code - FunFunFunction #53
Fun Fun Function
Throw out your tools - MPJ's Musings - FunFunFunction #54
Fun Fun Function
Unit tests vs. Integration tests - MPJ's Musings - FunFunFunction #55
Fun Fun Function
Object.create - Object Creation in JavaScript P6 - FunFunFunction #57
Fun Fun Function
More on: Algorithm Basics
View skill →Related Reads
📰
📰
📰
📰
Your Webhook Returned 200 OK. Did the Event Actually Get Processed?
Dev.to · Andrew Lencmanis
🚀 7 Git Commands Every Developer Should Know (But Most Don't)
Dev.to · Darshan Raval
Demystifying libuv: The Secret Engine Powering Node.js
Dev.to · Manoj Khatri
What Is a Simple Request and When Does the Browser Send a Preflight Request?
Dev.to · Alireza Hassankhani
🎓
Tutor Explanation
DeepCamp AI