Function composition plays a very important role in functional programming and in haskell things get better with the $
operator.
$
AKA Application operator
helps us in avoiding parentheses during function, a quick type check relves:
1 2 

1


That implies (f x)
is same as (f $ x)
but it helps us in rewriting f (g (h x))
as f $ g $ h $ x
So, say we have:
1


We can rewrite it as:
1


It gets interesting with the function composition operator .
1 2 

We can rewrite the $
expression to:
1


or
1


Another example would be using the $
and .
with interact
.
1 2 

^ Input from the standard input device is passed to this function as its argument, and the resulting string is output on the standard output device.
Say, we need to accept multiple inputs from the standard input and add them up, we could do it as:
1


If we are trying the same in Prelude
, you must use lines
with it. (Better not to use interact
in GHCi
)
1


So, the thing to remeber is:
f $ x = f x
(f . g) x = f (g x)