What is the difference between the dot (.)
and the dollar sign ($)
?
As I understand it, they are both syntactic sugar for not needing to us
($)
allows functions to be chained together without adding parentheses to control evaluation order:
Prelude> head (tail "asdf")
's'
Prelude> head $ tail "asdf"
's'
The compose operator (.)
creates a new function without specifying the arguments:
Prelude> let second x = head $ tail x
Prelude> second "asdf"
's'
Prelude> let second = head . tail
Prelude> second "asdf"
's'
The example above is arguably illustrative, but doesn't really show the convenience of using composition. Here's another analogy:
Prelude> let third x = head $ tail $ tail x
Prelude> map third ["asdf", "qwer", "1234"]
"de3"
If we only use third once, we can avoid naming it by using a lambda:
Prelude> map (\x -> head $ tail $ tail x) ["asdf", "qwer", "1234"]
"de3"
Finally, composition lets us avoid the lambda:
Prelude> map (head . tail . tail) ["asdf", "qwer", "1234"]
"de3"
My rule is simple (I'm beginner too):
.
if you want to pass the parameter (call the function), and $
if there is no parameter yet (compose a function)That is
show $ head [1, 2]
but never:
show . head [1, 2]
I think a short example of where you would use .
and not $
would help clarify things.
double x = x * 2
triple x = x * 3
times6 = double . triple
:i times6
times6 :: Num c => c -> c
Note that times6
is a function that is created from function composition.
One application that is useful and took me some time to figure out from the very short description at learn you a haskell: Since:
f $ x = f x
and parenthesizing the right hand side of an expression containing an infix operator converts it to a prefix function, one can write ($ 3) (4+)
analogous to (++", world") "hello"
.
Why would anyone do this? For lists of functions, for example. Both:
map (++", world") ["hello","goodbye"]`
and:
map ($ 3) [(4+),(3*)]
are shorter than map (\x -> x ++ ", world") ...
or map (\f -> f 3) ...
. Obviously, the latter variants would be more readable for most people.
All the other answers are pretty good. But there’s an important usability detail about how ghc treats $, that the ghc type checker allows for instatiarion with higher rank/ quantified types. If you look at the type of $ id
for example you’ll find it’s gonna take a function whose argument is itself a polymorphic function. Little things like that aren’t given the same flexibility with an equivalent upset operator. (This actually makes me wonder if $! deserves the same treatment or not )
The $
operator is for avoiding parentheses. Anything appearing after it will take precedence over anything that comes before.
For example, let's say you've got a line that reads:
putStrLn (show (1 + 1))
If you want to get rid of those parentheses, any of the following lines would also do the same thing:
putStrLn (show $ 1 + 1)
putStrLn $ show (1 + 1)
putStrLn $ show $ 1 + 1
The primary purpose of the .
operator is not to avoid parentheses, but to chain functions. It lets you tie the output of whatever appears on the right to the input of whatever appears on the left. This usually also results in fewer parentheses, but works differently.
Going back to the same example:
putStrLn (show (1 + 1))
(1 + 1)
doesn't have an input, and therefore cannot be used with the .
operator.show
can take an Int
and return a String
.putStrLn
can take a String
and return an IO ()
.You can chain show
to putStrLn
like this:
(putStrLn . show) (1 + 1)
If that's too many parentheses for your liking, get rid of them with the $
operator:
putStrLn . show $ 1 + 1