Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What does . (dot) stands in haskell?

What does . (dot) stands in haskell?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 -> Bool

and you want to define your own myOdd :: Int -> Bool function 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 . even

The myOdd functions behave exactly the same, but the second one is
created by "glue-ing" two functions together.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading