Skip main navigation

Dollars in Haskell

Haskell provides mechanisms to control the order of evaluation in functional expressions. This video explores the dollar operator ($).
6
As you see me here with my big dollar balloon, you might be thinking, aha, he’s going to talk about how to make money from Haskell, how to get rich quick by functional programming. And indeed, banks like Standard Chartered are hiring Haskell developers as fast as we can churn them out from the university here at Glasgow. A recent StackOverflow survey suggests that Haskell developers can expect to earn 100 thousand pounds a year, which is an awful lot of money. But I don’t really want to talk about money. In fact, I want to spend this video discussing the dollar operator and how you use it in your Haskell programs. In the same way that you use operators like + and -
53.5
for arithmetic and : and ++ for lists, the dollar operator is another way of handling expressions in the Haskell programming language. Let me try and motivate it in this way… Suppose you see this expression let f x = x squared (the little hat is the power-of sign, so that’s x squared) let f x = x squared in f 2 + 2
87.5
and what you’re thinking is: I wonder whether this means (f 2) + 2 or f (2 + 2) It’s all about where the implicit brackets would go. Why don’t you try typing this expression into the TryHaskell runtime interpreter, and see what answer you come back with…
112
aha, if you’ve done it, you’ll find out the answer is 6 So you get f 2 - which is 2 squared, which is 4, plus 2, which makes 6 The other possible alternative, the incorrect answer is f the implicit brackets there (2+2) which would give us 4 squared which is 16 But the brackets aren’t really there implicitly the brackets are like this because function application has a higher precedence or binds more tightly than all the other inbuilt operators in Haskell So whenever you see a function application you know it eats up the next thing it sees and then the rest of the expression is evaluated later. This is where the dollar operator comes in.
159.5
Perhaps you want to evaluate 2+2 and apply that whole expression as the argument of the f function here. So when you write $ that says evaluate the right hand side 2+2 which gives us 4 and then use that as the argument for the function f. Dollar means apply to this right hand side the function f So you see, dollar is all about productivity after all, because rather than two keystrokes, the left and right parentheses you’ve only got one now, for the dollar. Here we are… So Haskell makes you more productive, as well as possibly making you rich Keep on coding!

The dollar operator is useful for greater flexibility in function application, although you could use more brackets instead.

There are further details about dollars on the Learn You a Haskell site, although you haven’t yet been introduced to all the concepts they describe.

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