Skip to 0 minutes and 8 secondsWIM: Hello everyone. In this short lecture, I want to explain some basics of Haskell through examples from other languages that you might know. I will explain expressions, functions, types, and lists just through example without any deep detail. So first, expressions. In almost any programming language, you can write expressions such as, for example-- and you can bind these to variables so that this expression value is contained in that variable. In Haskell, you can do the same thing, and what is more, in Haskell there are only expressions, whereas in most imperative languages there are statements and expressions. Haskell only has expressions. So the next basic element of Haskell is, of course, functions.

Skip to 1 minute and 0 secondsAnd we can compare functions in Haskell, for instance, with functions in Python. In Python you might write something like-- so you define a function, hello, which takes an argument, name, and you return a string composed of the string, hello, and combined with name. So in Haskell you can do something quite similar, only a bit shorter.

Skip to 1 minute and 42 secondsSo Haskell has very compact syntax where you don't need parentheses to identify function arguments. You just use spaces. And the concatenation operator is separate from the addition operator.

Skip to 2 minutes and 13 secondsThe next basic element of Haskell is types. Types are very important in Haskell, and we will talk a lot about it, but for now we just want to give an example based on types in languages that you might know like C or Java. So for example, in C you could define a function like this.

Skip to 2 minutes and 42 secondsSo we have a function that takes two integers and returns an integer. So in Haskell we can write something similar, but the main difference is that the type declarations and the function definitions are separate. So we start with the type declaration. That would be-- so the double colon indicates that a type declaration follows. And then we have the first argument is an integer. The second argument is an integer. The return value is an integer. Then the function definition.

Skip to 3 minutes and 20 secondsSo again, essentially the same thing but much more compact. So the final basic element of Haskell that I want to discuss is lists. In many languages, such as Ruby, JavaScript, and Python, you can write lists like this.

Skip to 3 minutes and 44 secondsAnd, in fact, in Haskell this is exactly the same. So this is valid Haskell code. So lists are a very important component of the Haskell language. We'll talk a lot more about them in future classes.

Basic Elements By Example

 Expressions

In almost all programming languages you can create expressions such as:

    (b*b-4*a*c)/2*a

and you can assign these expressions to variables:

    v = (b*b-4*a*c)/2*a

In Haskell, you can do this as well, and what’s more: expressions are really all there is, there are no statements.

Functions

In Python, you can define a function such as

    def hello(name):
        return "Hello, "+name

In Haskell you can write this simply as:

    hello name = "Hello, "++name

Types

C has types, for example:

    int f (int x, int y) {
        return x*y+x+y;
    }

Haskell has much more powerful types than C, and we will talk a lot about types:

    f :: Int -> Int -> Int
    f x y =  x*y+x+y

Lists

In many languages, e.g. Python, JavaScript, Ruby, … you can create lists such as:

    lst = [ "A", "list", "of", "strings"]

Haskell also uses this syntax for lists.

To join lists, in Python you could write

    lst = [1,2] + [3,4]

In Haskell this would be very similar:

    lst = [1,2] ++ [3,4]

Anonymous functions

In JavaScript you can define anonymous functions (functions without a name) such as:

    var f = function(x,y){return x*y+x+y};

In Haskell, such anonymous functions are called lambda functions and they are actually the basis of the language. Again, the syntax is very compact:

    f = \x y -> x*y+x+y

Higher-order functions

Finally, in many languages, functions can operate on functions. For example, in Perl you can modify the elements in a list using:

    map sub ($x){$x*2+1}, [1..10]

Haskell provides many of these so-called higher-order functions, and lets you define your own.

    map (\x -> x*2+1) [1..10]

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: