So i currently have this function for generating all possible combinations
func :: [a] -> [b] -> [(a, b)]
func xs ys = concat(map (\x -> map (\y -> (x,y))ys) xs)
Now I want to extend it so that i can filter it using an anonymous function as parameter.
Example:
func [1,2,3] [4,5] (\ a b -> a+b > 6) -> [(2,5),(3,4),(3,5)]
I tried to change the Typesignature of func:
from: [a] -> [b] -> [(a, b)]
to: [a] -> [b] -> (a1 -> a1 -> Bool) -> t
But it did not work.
Thank you in advance!
OP has stated that they changed their code to
followed by what one assumes is
which gives them the error
This is easy to fix; in fact the error message is relatively clear as GHC error messages go. (That's faint praise, admittedly.) Doing this instead should work, though I'm away from a Haskell compiler and can't run it:
Explanation:
\a b -> a + b > 7
is an anonymous function taking two arguments,a
andb
, whereas\(a, b) -> a + b > 7
is an anonymous function taking a single argument which is a pair. The type of the last argument offunc
,((a, b) -> Bool)
, is the type of the second anonymous function, not the first.(This problem can also be fixed by making
func
's last argument be of typea -> b -> Bool
, while passing the first anonymous function.)I originally thought OP's
tup
was justzip
, and recommended they usezip
instead to improve legibility. OP correctly points out that their function has very little in common withzip
except the type. There may still be a legibility win to be had, though; see this.