Haskell, a beginners guide to learn a functional programming language | Laurenz Dilba | Skillshare

Playback Speed


  • 0.5x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 2x

Haskell, a beginners guide to learn a functional programming language

teacher avatar Laurenz Dilba, Programming made easy

Watch this class and thousands more

Get unlimited access to every class
Taught by industry leaders & working professionals
Topics include illustration, design, photography, and more

Watch this class and thousands more

Get unlimited access to every class
Taught by industry leaders & working professionals
Topics include illustration, design, photography, and more

Lessons in This Class

11 Lessons (17m)
    • 1. Introduction

      1:02
    • 2. Software and Setup

      1:09
    • 3. Functions and Types

      1:15
    • 4. Recursive Functions

      2:35
    • 5. Conditions

      1:33
    • 6. Local Variables

      2:13
    • 7. Wildcards

      1:36
    • 8. Parametric Types

      1:07
    • 9. Custom Types

      1:57
    • 10. Recursive Lists

      1:52
    • 11. Conclusion

      0:38
  • --
  • Beginner level
  • Intermediate level
  • Advanced level
  • All levels
  • Beg/Int level
  • Int/Adv level

Community Generated

The level is determined by a majority opinion of students who have reviewed this class. The teacher's recommendation is shown until at least 5 student responses are collected.

110

Students

--

Projects

About This Class

5b0e9584

Did you ever thought, it takes way too long, to get a simple project sketch? Or did you ever wanted to learn something different than the common programming languages like Java, C# etc?

I want to introduce you to a new world of functional pragramming. It's benefitial to know the basics of programming, otherwise this course may be a bit too fast paced for you.

As the Haskell compiler is very strict, you may come across erorrs like "variable not in scope". Double and triple check your spelling, spaces and brackets.

And the most important thing, code yourself. You learn the most if you apply everything you learned directly. During the course I provide you with simple challanges. 

Let's get coding!

Meet Your Teacher

Teacher Profile Image

Laurenz Dilba

Programming made easy

Teacher

Hello, I'm Laurenz. I am a German data scinece student and I am creating tutorials, mainly about Java, since two years. I hope that I can introduce you into coding or improve your knowledge.

See full profile

Class Ratings

Expectations Met?
  • Exceeded!
    0%
  • Yes
    0%
  • Somewhat
    0%
  • Not really
    0%
Reviews Archive

In October 2018, we updated our review system to improve the way we collect feedback. Below are the reviews written before that update.

Why Join Skillshare?

Take award-winning Skillshare Original Classes

Each class has short lessons, hands-on projects

Your membership supports Skillshare teachers

Learn From Anywhere

Take classes on the go with the Skillshare app. Stream or download to watch on the plane, the subway, or wherever you learn best.

Transcripts

1. Introduction: Hey, guys. And welcome to this Haskell's girl Check cars. This course I want to teach you the basics of fiscal, starting with types and simple functions and building up on that as we go further. Haskell is the language for those who want short code for simple functions. You should have basic knowledge off Ryker Shin and programming in general to follow this course more easily. But I'll show you everything you need to know to get started. Profess Co First, Haskell is a function of programming language. This means to compile a decides which function to call or when to call it. You can control the flow of your program. This may seem kind of restricting, but offers new possibilities and makes a lot of things easier. The most important thing is there no so called side effects. When you write a function, the result off it will always be the same. When you put in the same value, I will teach you more about it when we write our own functions. In general, this means that our functions will be shorter, clearer to read more consistent and faster 2. Software and Setup: during this course I will use to programs as it caught editor. I will use atom, but you can use whatever you like. Even the most basic text editor. We work to compile the program. I will use the Hasker platform. It includes Lee Haskell Glasscock compiler and has user interface. To simplify things, I've included length to both in the course description. Once you've downloaded and installed both programs start Atom and installed the Haskell language support. You can do that by simply clicking on download package on the Welcome Pitch Search for Hess Co. And in Salt Language minus Haskel. After that created new file and safe with the ending dot hs No open win GH c I and click on the open file icon on the top left, navigate to your Haskell file and click open. You can enter natively right? The comments that appeared on the top Your file is no successfully loaded by clicking the green arrows. You can update your fire after you make changes to it. That is everything we need to get started 3. Functions and Types: as our first function. We want a function that calculates the square of a given number and beware Heskey, less case sensitive and space sensitive to declare a function, simply write down the functioning in our case Square followed up by the pyramid is we want to give that function separated by spaces. We only have one perimeter, the number we want to be scrapped. The result off the function is written behind unequal son. In our case, the result is X Times X now in the Hasker console. Refresh our file and type the name off the function, followed by the number we want to give it. Every function has a type to let the Haskell compiler calculate its type right, Colin type followed by the function name in the Consul. But we can declare it manually as well. We need to know what type the perimeter is and what the results connect. Every argument with Videsh and the greater sign. So we get something like an arrow and we're done. This step is necessary, but it helps to get a better overview off what the function is supposed to do. 4. Recursive Functions: a big part off Haskell is rickerson, as we don't have loops to iterated through data structures. If you're familiar with recurring to stay, it might be easier for you, but I'll charge you the basics that you need to know. Let's say we want the function that calculates the length of a given list. The general idea is to add one toe. Our result as long as the list is not empty, and after we edit one, we remove one item from the list to write a listing. Has killed were three options. The first is to put an element into briquettes. The second is to insert an element in a break it, and the third and one we're going to use for this function is the first element X taken from the list X s. So let's write our length function esta Cuba length. It's used by Haskell itself. We're going to use Len. The input is a list off Inter just and the output is an inter job. The lengths off the list, as I mentioned before our base case for the Ricker Shin is an empty list. Then the length is equal to zero and for the Ricker Shin part. We want to split our list into a head and a tape part for each iteration. We want to get one and call the function lent again, but this time with only the tail part as the perimeter. This way, the list were D crimen in each iteration, and the Ricker Shin will terminate. Let's test of Our function works our next function, gets an end as a perimeter and returns a list off Inter just starting from the first perimeter. Let's call it get list. If you write, get list for we should get a list roof the elements for 3 to 1. I highly suggest you try it by yourself. Pastas Video No and give it a shot. So first of all, let's declare the function time. It takes an integer as the perimeter and returns a list off Inter. Just next, our base case for the rickerson. If our perimeter X zero will return an empty list if X is not zero, we want to insert X into the list that it's returned by the function itself in the next iteration, and we want to de crimen X by one so that the function terminates. This may seem kind of complicated at first, but the more functions you write, the easier it gets. 5. Conditions: if we want to write a function, which includes a condition, Haskell offer some easy to use tools. There. Two options you can use, I told your boss in a simple example. That's right, a function that takes into interjects and returns to greater one of those. The first option we have is to ride the function with the if then else notation. In our case, we check of X is greater or equal to white. In that case, re return X Otherwise, who returned? Why, If you want to check for multiple cases, I've dissertation gets too complicated to read. We have another option. Instead, off the first equal Thanh rewrite this vertical bar at the first condition after that. In this case, it's the same s we had before. For every other condition we can write vertical bar as well. For final else case, we're used to keyword otherwise, as I mentioned before. If X is not greater than why, Rutan, why justice in Beside fect has good supports, two pills and triples and so on a swell the notation is pretty straightforward. Simply put the arguments in the break it and separate them back commas its assent procedure with the type declarations 6. Local Variables: let's combine nearly everything we learned so far and build a quiet, more complicated function in algebra. We often have functioned that intersect with the ex excess to calculate where the white coordinates gets to zero. We're going to use the quadratic formula for this. I put it on the screen for you. We're going to use local variable declarations to summarize some parts off it. The square root off be cubed minus four. A. C is called D in our function two times a is called E. This way we can write the plus minus part more easily in the rear function. We have three parameters. A has a cubed X B has an X. We leave out that part as it's always the same, and we can take it for granted. So let's build our function. Let's color function roots. It takes three flowed values as perimeters and returns the tupelo off to floats off our roots. So routes have the pyramid nous a B C. Our first route is the negative part, minus B minus. D divided by and our positive part is the same. But with the plus sign. Next, we need to clarify what d and e are supposed to mean and Haskell. We have two key word. Where for the task. Simply ride with the equal. Describe Rode off be times B minus four times a time, see and E equals to two times a. Let's open up the consul and refresh our file and test of our function route works. 7. Wildcards: typically radically each argument with a name like X A wire. But if they are necessary in our function, we can use the so called wild card. It's written with an underscore on Haskell. That means we can put in whatever we want. And the Haskell compiler simply ignores that argument. To demonstrate the usefulness off dead, we'll write a custom and faction if explained, Why are the same? The result is true. In every other case, the result is false. Let's name dysfunction, my end s Dickie What and is taken by Haskell. It takes two billions as perimeters and returns to bullion a swell If the first argument is true, the result is true Whatever wise if why is true, the result is true And if why is false to result this obviously for this well for the next case, If X is anything other than true, we can simply ignore why and return faults In cases like this, this wildcat is fairly useful to demonstrate where this white cut is necessary. Really write a function that checks if the length of a given list three as the perimeter, it gets list off inter GIs and returns a 1,000,000,000 if we pass exactly three arguments X , Y and Z The lengths off the list is obviously three in every other case, the length of not three and the function returns faults for this every other case we use a white card. 8. Parametric Types: If you ever typed Kahlan type in the Haskell concert, you probably stumbled across types like a or B instead off Indian bullion. This is the most in general type, off each argument a couple episodes before he wrote a function that calculates the length of a given list. If you remember, we could only get the length off lists, which contained Inter just. But what if our list has, Let's say billions. In that case, we can replace the end within a in the type declaration off dealing function. Before I tell you the mother fat length function dried yourself pastas for you know and write a length function but within a instead often end in the type declaration. So the function then takes an array off ace and returns an end. After that, it's the same as our last lang function. If the list is empty the length of zero in every other case, we split our list in the head and the tear apart. The result. It's one edit to the lengths off the tear apart 9. Custom Types: If the data types we have are insufficient anymore, we have the option to declare our own types. If you want to model a traffic light, it's getting complicated. If we want to use billions and inter dress and so on. With the keyword data followed up by the data type name, we can declare our own type constructor. Let's say we want the data type color, which can be either red, yellow or green with deriving show. We can print out all objects. This part is optional about useful, most off the time for our function T light, which stand for traffic light. We want a color as the perimeter and return a color as well. If our traffic light is red, it will be green and next step. If it's green, it will be yellow. And let's say in every other case it will be read. The next challenge for you is to write a custom bullion type and a correlating and function . It should be have exactly like the normal function, but should use your own custom bullion type process video No and try it. Let's declare our custom bullion right data my boo to construct the type It e quits to my true on my faults and it's arriving show as well the function see end which stands for custom and takes to my bull as perimeters and returns in my boot. If X is my true, the result us whatever white is if X is anything other than my true, the result is my faults. 10. Recursive Lists: our daughter types can have pyramid us as well. I'll show you a way to write Rikers of custom list object. The general idea off a Rikers off list is that every element has a link to the next element until the last element isn ill or empty data list takes the Perimeter A and a sequel to ever Know the empty list, Our concept A and the link to the Next List, which is represented by the Argument list A. The list object is deriving show. I'll tell you away how you can depend to lists, and after that you can try to right the function, which calculates the length off are recursive list the function. Epp takes two lists and return to list, which is the combination of both. If our first list is no, the combination off both nurses, obviously, the second list only if our first list is a constant, often excess, the perimeter and an excess. As a link to the next list, we take context and apply the app function to the list. X s and y s okay, let's try out if our function works now, try to write another length function, but this time with our custom dist. The Lend function takes a list object of a perimeter. A on returns an end. If our list it's no the length zero. If we have accounts with the less excess appended to it, we're at one toe our result and call in again with the list dependent to it. 11. Conclusion: That's it for this Haskell beginner cars. You learn pretty much so far. But as with all programming languages, there's so much more you can done and explore. Yes, this was a beginner's course. I have uncovered a couple things you can learn about anonymous functions, higher functions, infinite data structures, Korean and pattern matching, how the compiler works and so on and so on. But I hope that this video course gave you an overview. What you can achieve with Haskell. If you liked it, please read and share it. If you have any questions, post them down below and I'll try to answer them as quickly as I can. Thank you for watching and us here then.