Eliminating parentheses in Haskell with ‘$’

A fun thing about looking at different programming languages is that you get to see the unique features of each language. For instance, some people don’t like Lisp because of all of the parentheses, and then Haskell seems to counter that by saying, “Hey, here are a couple of ways to get rid of parentheses.”

Haskell’s $ operator

One of the approaches Haskell uses is the $ function, which is called the function application operator. As an example of how it works, imagine that you want to calculate the square root of three numbers. This first approach is intentionally wrong because it calculates the square root of 3, then adds that to 4, then adds that to 2:

let x = sqrt 3 + 4 + 2     # intentionally wrong

The usual way to correct this is with parentheses: you need to make sure the three numbers are added together first, before the sqrt function is applied. As with other programming languages, Haskell lets you write this:

let x = sqrt (3 + 4 + 2)

For the first 20+ years of my programming life I wrote code like that, but Haskell says, “You know what, those parentheses are kind of noisy, let’s get rid of them.” The way you get rid of them is by using $ instead:

let x = sqrt $ 3 + 4 + 2

Whoa, that’s different.

The first 50 or 100 times you see that it’s pretty freaky, but over time it grows on you. As the book, Learn You a Haskell for Great Good states, you can think of $ as being like an “open parenthesis.”

Comparing parentheses to $

IMHO, the $ is a little more attractive when you compare it to the same expression that uses parentheses. For instance, these two lines are equivalent, and the second line is a little easier on the eyes:

sum (map sqrt[1..10])
sum $ map sqrt[1..10]

The same is true of these two lines of code:

putStr (map toUpper "rocky raccoon")
putStr $ map toUpper "rocky raccoon"

Until I saw this approach, I never realized how much noise parentheses add to an expression.

Read from right to left

Here’s one more example:

let x = negate(sqrt(3 + 4 + 2))
let x = negate $ sqrt $ 3 + 4 + 2

With an expression like this, you basically read the code from right to left. That last line can be read like this:

  1. Sum up “3 + 4 + 2”               // 9
  2. Take the square root of that     // 3
  3. Negate that value                // -3
  4. Bind that result to x            // x = -3

Because of the way it works, we say that $ is right-associative.

At the moment I don’t know how the Haskell creators came up with the $ symbol. When I read code like this it reminds me of reading Unix command pipelines, except that you read them from right to left. That makes me think that the | symbol would have been cool here.

Summary

I don’t have a major summary here, I just wanted to show a few examples of how to use Haskell’s $ operator (which is technically a function). The $ in Haskell has at least one more use that I’m aware of, and I’ll try to write about that at some point.

(Note: The examples on this page were inspired by the book, Learn You a Haskell for Great Good.)