2

I am trying to generate all possibles combinations from two lists and then only return those that match the lambda function. So I currently have the following code:

func :: [a] -> [b] -> ((a, b) -> Bool) -> [(a, b)]
func xs ys f = filter f (tup)
    where
        tup = concat(map (\x -> map (\y -> (x,y))ys) xs)

Currently I am able to generate all the possible combinations, but the filtering won't work.

Error for the input : func [1,2,3] [4,5] (\ a b -> a+b > 6)

• Couldn't match expected type ‘Bool’ with actual type ‘(a, b) -> Bool’

• The lambda expression ‘\ a b -> a + b > 7’ has two value arguments, but its type ‘(a, b) -> Bool’ has only one

How can I solve this?

I tried to use map instead of filter, but that did not work as well.

1
  • 2
    You almost got it, try func [1, 2, 3] [4, 5] (\(a, b) -> a+b > 6) instead. Commented Nov 24, 2022 at 14:36

1 Answer 1

4

Making a function with two arguments and making a function with a single 2-tuple argument use slightly different syntax:

\a b -> a+b > 6 -- two arguments
\(a, b) -> a+b > 6 -- one tuple argument
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.