0:07Skip to 0 minutes and 7 secondsWIM: Hello, everyone. In this short video, we're going to look at some more basic elements of Haskell by examples from other languages. So we will look at anonymous functions, higher-order functions, blocks, and conditions. First, anonymous functions. So these are functions that don't have a name and they occur in many languages. For example, in JavaScript. So in JavaScript you can write something like-- so what we have here is a variable f that contains a complete function. In Haskell, these anonymous functions are called lambda functions and they're really very important. They are actually the foundation of the language. So there is a very compact syntax for that. Again, in Haskell the same example would look as follows.

1:05Skip to 1 minute and 5 secondsAnd again, we will talk a lot more about lambda functions in the next class. So another important element in many programming languages is the block structures that you use to define functions. For example, again in JavaScript we could define a function that returns the roots of a quadratic equation. So-- we have a function that takes the coefficients of the quadratic equation and then we can compute the roots in the familiar way. So we have-- so we have a function that defines a block of code where we find a number of variables. So first we have the square of the determinant. Then we take the square root to get to the determinant.

2:09Skip to 2 minutes and 9 secondsThen we compute the root and the first root of the quadratic equation and the second root and we return them both in a list. So, in Haskell, this same code looks very similar. And we will just define it by removing some of this syntax.

2:39Skip to 2 minutes and 39 secondsSo we have a function of a, b, and c. And the function is actually defining a block using a let construct. So it says let and then a number of variables in the expression of this return. Because the whole thing is an expression, you don't need the return statement.

3:00Skip to 3 minutes and 0 secondsAnother very important construct in any programming language is a conditional construct. So an if-then, or if-then-else. For example, in Python we might define a function to compute the maximum of two numbers as follows.

3:24Skip to 3 minutes and 24 secondsSo the function max takes two values, x and y, and we do a comparison. If x is greater than y, we return x. And otherwise, we return y. So nothing special there. Haskell has a very similar if-then statement, but again it's an expression, not a statement. So let's look at what it looks like.

3:57Skip to 3 minutes and 57 secondsSo the last feature of Haskell that I want to illustrate is actually a little bit less basic-- a little bit more advanced-- it's called higher-order functions. These are functions that operate on other functions, or functions that take functions as arguments. And again, we can compare this with other languages. In this case, we compare it with Perl. So, in Perl you could, for instance, compute the double of a list using the following code.

4:31Skip to 4 minutes and 31 secondsSo what happens here is that we have an anonymous function that takes an argument and doubles it, and the map function makes this anonymous function work on the list from 1 to 10. So again, in Haskell, this is very similar.

4:53Skip to 4 minutes and 53 secondsLike that. So we have a map, which takes an anonymous function. So we have map, which takes an anonymous function and works on the list, and it will double every element in the list. So these higher-order functions are a lot more used in a functional language like Haskell than in an imperative language, but, as you can see, they do exist in other languages as well.

More Basic Elements by Example

A few more basic (and not-so-basic) elements of Haskell through comparison with other languages. We will not go into detail on the Haskell constructs, just show the similarities with constructs from languages you may know.