Dot Operator in Haskell: need more explanation

后端 未结 6 1052
旧时难觅i
旧时难觅i 2020-11-30 23:50

I\'m trying to understand what the dot operator is doing in this Haskell code:

sumEuler = sum . (map euler) . mkList

The entire source code

6条回答
  •  生来不讨喜
    2020-12-01 00:18

    sum is a function in the Haskell Prelude, not an argument to sumEuler. It has the type

    Num a => [a] -> a
    

    The function composition operator . has type

    (b -> c) -> (a -> b) -> a -> c
    

    So we have

               euler           ::  Int -> Int
           map                 :: (a   -> b  ) -> [a  ] -> [b  ]
          (map euler)          ::                 [Int] -> [Int]
                        mkList ::          Int -> [Int]
          (map euler) . mkList ::          Int ->          [Int]
    sum                        :: Num a =>                 [a  ] -> a
    sum . (map euler) . mkList ::          Int ->                   Int
    

    Note that Int is indeed an instance of the Num typeclass.

提交回复
热议问题