0
votes

If I have this type signature, what function should I write to satisfy it?

allPair :: [a -> b -> Bool] -> [(a,b)] -> Bool 

The only thing I know is == has type a -> a -> Bool.

It returns True if all the functions applied to their corresponding pair values return True - otherwise False. It also returns false if there are different numbers of functions and pairs.

1
There are too many possible things that that function could do. The name of the function alone is not enough; you should describe its behaviour. - user2407038
It returns True if all the functions applied to their corresponding pair values return True - otherwise False.It also returns false if there are different numbers of functions and pairs. - lee1234567890

1 Answers

1
votes

There are many possible implementations, but it seems like the one you are reaching for is a function that takes the first element of the [a -> b -> Bool] list and applies it to the values taken from the first element of the [(a, b)] list to produce a Bool which will be &&-ed together with the analogous outputs from the second elements, third elements, and so on.

Here is a function to do this, under the assumption that the base case of no data evaluates to True, and that whenever there are more functions than arg-pairs, or more arg-pairs than functions, it should be False.

allPair :: [a -> b -> Bool] -> [(a, b)] -> Bool
allPair [] [] = True
allPair _ [] = False
allPair [] _ = False
allPair (f:fs) ((a,b):vals) = (f a b) && (allPair fs vals)

for instance

Prelude> allPair [(>), (<)] [(3, 1), (10, 2)]
False
Prelude> allPair [(>), (<)] [(3, 1), (2, 10)]
True

Note that this function assumes the arguments passed to it will be of compatible lengths (or that getting False back when they aren't is acceptable). It is quite tricky and involves language extensions if you want to actually use the type system to enforce matching arity of List arguments.