I have a function cc declared as following (1st parameter is a (list) and 2nd parameter is b (list as well), it should return 3rd list:
cc :: [(String, String)] -> [(String, String)] -> [(String, String)]
cc a b = do
Example:
a = [("aaa", "xxx"), ("bbb", "xxx")]
b = [("xxx", "ccc"), ("xxx", "ddd")]
c should be [("aaa", "ccc"), ("aaa", "ddd"), ("bbb", "ccc"), ("bbb", "ddd")]
c is composition of a and b where each a pairs second “index” is b pairs first “index”.
So a (“aaa”, “xxx”) pair’s second “index” is “xxx” and its defined as b (“xxx”, “ccc”) first “index”. Regarding to that we create(add) this new pair (“aaa”, “ccc”) to the return list.
Question is how to do that in Haskell? 🙂
Best regards!
The simplest way to do that is a list comprehension,
we pair up all elements of
awith all elements ofband filter according to the condition, assembling the result from the parts.