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

前端 未结 6 1913
旧时难觅i
旧时难觅i 2020-12-14 17:45

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

decades         :: (RealFrac a) => a -> a -> [a] -> Array Int Int
decades a b     =         


        
相关标签:
6条回答
  • 2020-12-14 18:24

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

    0 讨论(0)
  • 2020-12-14 18:29

    It is a function composition: link

    0 讨论(0)
  • 2020-12-14 18:30

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

    0 讨论(0)
  • 2020-12-14 18:34

    f(g(x))

    is

    in mathematics : f ∘ g (x)

    in haskell : ( f . g ) (x)

    0 讨论(0)
  • 2020-12-14 18:40

    . 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
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题