Monday 6 June 2016

Haskell: Check whether all the elements in a list are even (or) not.

Following statement returns True, if all the elements in the list are even, else False.

allEven list = (list == [x | x <- list, even x])
Prelude> let allEven list = (list == [x | x <- list, even x])
Prelude> 
Prelude> allEven [4, 5, 6]
False
Prelude> allEven [4, 8, 6]
True
Prelude> allEven []
True


Previous                                                 Next                                                 Home

No comments:

Post a Comment