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

14,310

Solution 1

f(g(x))

is

in mathematics : f ∘ g (x)

in haskell : ( f . g ) (x)

Solution 2

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.

Solution 3

. 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

Solution 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'

Solution 5

It is a function composition: link

Share:
14,310

Related videos on Youtube

Casebash
Author by

Casebash

Bachelor of Science (Adv Maths) with Honors in Computer Science from University of Sydney Programming C/C++/Java/Python/Objective C/C#/Javascript/PHP

Updated on April 16, 2022

Comments

  • Casebash
    Casebash about 2 years

    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)
    
    • kennytm
      kennytm about 14 years
      A period can also be a namespace separator (e.g. Data.Vector.Unboxed.length).
    • Antal Spector-Zabusky
      Antal Spector-Zabusky about 14 years
      For searching for information about Haskell code, I heartily recommend Hoogle (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! (holumbus.fh-wedel.de/hayoo/hayoo.html), which has less of an emphasis on types but indexes more packages.
    • Don Stewart
      Don Stewart about 13 years
  • Alex Jenter
    Alex Jenter about 14 years
    You just need to remember that the function application operator (space) has the highest priority. After some time it will all make sense.
  • Sid
    Sid over 6 years
    Better suited as a comment.