What does . (dot) stands in haskell?
>Solution :
The . stands for function composition in Haskell.
It allows you to chain different functions. In your case, instead of doing a new function that calls toDigits and then sum over the result, you can use function composition.
sumDigits = sum (map myFunction)
myFunction xs = sum (toDigits xs)
The definition of . is the following.
(.) :: (b -> c) -> (a -> b) -> a -> c
f . g = \x -> f (g x)
There’s a really good example on stackoverflow already you can check.
Say you have these functions:
even :: Int -> Bool not :: Bool -> Booland you want to define your own
myOdd :: Int -> Boolfunction using
the two above.The obvious way to do this is the following:
myOdd :: Int -> Bool myOdd x = not (even x)But this can be done more succinctly using function composition:
myOdd :: Int -> Bool myOdd = not . evenThe myOdd functions behave exactly the same, but the second one is
created by "glue-ing" two functions together.