Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does a fullstop or period or dot (.) mean in Haskell?

I really wish that Google was better at searching for syntax:

decades         :: (RealFrac a) => a -> a -> [a] -> Array Int Int
decades a b     =  hist (0,9) . map decade
                   where decade x = floor ((x - a) * s)
                         s        = 10 / (b - a)
like image 465
Casebash Avatar asked Sep 06 '25 13:09

Casebash


2 Answers

f(g(x))

is

in mathematics : f ∘ g (x)

in haskell : ( f . g ) (x)

like image 133
Pratik Deoghare Avatar answered Sep 10 '25 05:09

Pratik Deoghare


It means function composition. See this question.

Note also the f.g.h x is not equivalent to (f.g.h) x, because it is interpreted as f.g.(h x) which won't typecheck unless (h x) returns a function.

This is where the $ operator can come in handy: f.g.h $ x turns x from being a parameter to h to being a parameter to the whole expression. And so it becomes equivalent to f(g(h x)) and the pipe works again.

like image 33
Alex Jenter Avatar answered Sep 10 '25 07:09

Alex Jenter