26

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)
Don Stewart
  • 137,316
  • 36
  • 365
  • 468
Casebash
  • 114,675
  • 90
  • 247
  • 350
  • 2
    A period can also be a namespace separator (e.g. `Data.Vector.Unboxed.length`). – kennytm Mar 21 '10 at 09:59
  • 8
    For searching for information about Haskell code, I heartily recommend Hoogle (http://www.haskell.org/hoogle/), a search engine for types (e.g. searching for `(a -> b) -> [a] -> [b]` turns up `map`) and function/operator names (so searching for `map` turns up `map`, and searching for `.` turns up the Prelude function composition operator `(.)`). There's also Hayoo! (http://holumbus.fh-wedel.de/hayoo/hayoo.html), which has less of an emphasis on types but indexes more packages. – Antal Spector-Zabusky Mar 21 '10 at 20:10
  • 1
    possible duplicate of [Dot Operator in Haskell: need more explanation](http://stackoverflow.com/questions/631284/dot-operator-in-haskell-need-more-explanation) – Don Stewart Apr 18 '11 at 22:53

6 Answers6

49

f(g(x))

is

in mathematics : f ∘ g (x)

in haskell : ( f . g ) (x)

kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
Pratik Deoghare
  • 35,497
  • 30
  • 100
  • 146
18

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.

Community
  • 1
  • 1
Alex Jenter
  • 4,324
  • 4
  • 36
  • 61
  • 5
    You just need to remember that the function application operator (space) has the highest priority. After some time it will all make sense. – Alex Jenter Mar 21 '10 at 10:37
13

. 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
retronym
  • 54,768
  • 12
  • 155
  • 168
5

"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."

Bjorn
  • 69,215
  • 39
  • 136
  • 164
Alan
  • 45,915
  • 17
  • 113
  • 134
3

It is a function composition: link

ceth
  • 44,198
  • 62
  • 180
  • 289
1

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

Drakosha
  • 11,925
  • 4
  • 39
  • 52