Skip main navigation
We use cookies to give you a better experience, if that’s ok you can close this message and carry on browsing. For more info read our cookies policy.
We use cookies to give you a better experience. Carry on browsing if you're happy with this, or read our cookies policy for more information.

Skip 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.

Skip 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.

Skip 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.

Skip 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.

Skip 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.

Skip 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.

Skip to 3 minutes and 50 secondsAnd again, we need less syntax in Haskell than in Python.

Skip 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.

Skip 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.

Skip 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.

Blocks

In JavaScript functions typically are blocks of code:

    function roots(a,b,c) {
        det2 = b*b-4*a*c;
        det  = sqrt(det2);
        rootp = (-b + det)/a/2;
        rootm = (-b - det)/a/2;
        return [rootm,rootp]
    }

In Haskell, we would write this function as follows:

    roots a b c = 
        let
            det2 = b*b-4*a*c;
            det  = sqrt(det2);
            rootp = (-b + det)/a/2;
            rootm = (-b - det)/a/2;
        in
            [rootm,rootp]

Note that the let ... in ... construct is an expression, so it returns a value. That’s why there is no need for a return keyword.

Conditions

In Python we could write a function with a condition as like this:

def max(x,y):
    if x > y:
        return x
    else:
        return y

Of course Haskell also has an if-then construct:

    max x y = 
        if x > y
            then x
            else y

Again the if ... then ... else ... construct is an expression, so it returns a value.

Case statement

Many languages provide a case statement for conditions with more than two choices. For example, Ruby provides a case expression:

    Red = 1
    Blue = 2
    Yellow = 3

    color = set_color();
    action = case color 
        when Red then action1()
        when Blue then action2()
        when Yellow then action3()
    end

In Haskell, the case works and looks similar:

    data Color = Red | Blue | Yellow

    color = set_color
    action = case color of
        Red -> action1
        Blue -> action2
        Yellow -> action3

Note however how we use the type as the value to decide on the case, where in other languages we need to define some kind of enumeration.

Generics/Templates

In Java and C++ there are generic data types (aka template types), such as:

    Map<String,Integer> set = new HashMap<String,Integer>();

In Haskell, you would write this as follows:

    set :: Data.Map.Map String Integer 
    set = Data.Map.empty

The main difference is of course that set in Haskell is not an object but an immutable variable, so where in Java you would say:

    set.put("Answer",42)

In Haskell you would say:

    set' = insert "Answer" 42 set

Because in Haskell variables are immutable, the return value of the insert call is bound to a new variable rather than updating the variable in place as in Java.

Share this video:

This video is from the free online course:

Functional Programming in Haskell: Supercharge Your Coding

University of Glasgow

Get a taste of this course

Find out what this course is like by previewing some of the course steps before you join:

Contact FutureLearn for Support