开发者

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)


f(g(x))

is

in mathematics : f ∘ g (x)

in haskell : ( f . g ) (x)


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.


. is a higher order function for function composition.

Prelude> :type (.)
(.) :: (b -> c) -> (a -> b) -> a -> c
Prelude> (*2) . (+1) $ 1
4
Prelude> ((*2) . (+1)) 1
4


"The period is a function composition operator. In general terms, where f and g are functions, (f . g) x means the same as f (g x). In other words, the period is used to take the result from the function on the right, feed it as a parameter to the function on the left, and return a new function that represents this computation."

Source: Google search 'haskell period operator'


It is a function composition: link


Function composition (the page is pretty long, use search)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜