Want to keep learning?

This content is taken from the The University of Glasgow's online course, Functional Programming in Haskell: Supercharge Your Coding. Join the course to learn more.

Skip to 0 minutes and 6 seconds 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 -

Skip to 0 minutes and 54 seconds 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

Skip to 1 minute and 28 seconds 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…

Skip to 1 minute and 52 seconds 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.

Skip to 2 minutes and 40 seconds 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!

Dollars in Haskell

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.

Share this video:

This video is from the free online course:

Functional Programming in Haskell: Supercharge Your Coding

The 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: