Haskell function composition

后端 未结 6 1564
梦如初夏
梦如初夏 2021-02-01 14:42

I am reading this tutorial on Haskell. They define function composition as the following:

(.)                     :: (b->c) -> (a->b) -> (a-         


        
6条回答
  •  时光说笑
    2021-02-01 15:27

    This example is contrived, but suppose we have

    sqr x = x * x  
    inc x = x + 1
    

    and we want to write a function that computes x^2+1. We can write

    xSquaredPlusOne = inc . sqr
    

    (which means

    xSquaredPlusOne x = (inc . sqr) x
    

    which means

    xSquaredPlusOne x = inc(sqr x)
    

    since f=inc and g=sqr).

提交回复
热议问题