Skip main navigation

Basic Elements By Example

We introduce some basic elements of Haskell through comparison with other languages
WIM: 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.
And 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.
So 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.
The 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.
So 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.
So 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.
And, 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.


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


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.


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


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


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]
This article is from the free online

Functional Programming in Haskell: Supercharge Your Coding

Created by
FutureLearn - Learning For Life

Reach your personal and professional goals

Unlock access to hundreds of expert online courses and degrees from top universities and educators to gain accredited qualifications and professional CV-building certificates.

Join over 18 million learners to launch, switch or build upon your career, all at your own pace, across a wide range of topic areas.

Start Learning now