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 main navigation

More Basic Elements by Example

A few more basic (and not-so-basic) elements of Haskell through comparison with other languages.
7.1
WIM: 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.
65.2
And 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.
129
Then 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.
158.6
So 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.
179.7
Another 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.
204.1
So 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.
229.9
And again, we need less syntax in Haskell than in Python.
236.5
So 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.
271.4
So 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.
292.9
Like 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.
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 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' = Data.Map.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.
This article is from the free online

Functional Programming in Haskell: Supercharge Your Coding

Created by
FutureLearn - Learning For Life

Our purpose is to transform access to education.

We offer a diverse selection of courses from leading universities and cultural institutions from around the world. These are delivered one step at a time, and are accessible on mobile, tablet and desktop, so you can fit learning around your life.

We believe learning should be an enjoyable, social experience, so our courses offer the opportunity to discuss what you’re learning with others as you go, helping you make fresh discoveries and form new ideas.
You can unlock new opportunities with unlimited access to hundreds of online short courses for a year by subscribing to our Unlimited package. Build your knowledge with top universities and organisations.

Learn more about how FutureLearn is transforming access to education