Search code examples
haskellhaskell-platform

How to reimplement the "all" function in Haskell?


I need to define a functionall' :: ( a -> Bool ) -> [a] -> Bool which verifies if all the elements from a list satisfy a certain condition .

For example : all' ( <5) [1,2,3] = True , all' (>=2) [1,1,2,2,3,3] = False.

My main problem is that I don't know how to handle the transmission of a function.


Solution

  • Functions in Haskell are passed just like any other value. Here's an example to help you progress:

    doBothSatisfy :: (a -> Bool) -> a -> a -> Bool
    doBothSatisfy p x y = (p x) && (p y)
    

    And now the usage:

    doBothSatisfy (> 5) 6 7 == True
    doBothSatisfy (> 5) 1 8 == False
    

    Now try to extend that to lists.