how to transform two lists into a list of tuples in haskell

0

I have this list ["A","B","C","D"] and this other [["B","C"],["A","C"],["A","B","D"],["C"]] and I want to convert it to this [("A","B"),("A","C"),("B","A"),("B","C"),("C","A"),("C","B"),("C","D"),("D","C")] for each element of the first list creates a tuple with the first element of the second list is to say "A" with ["B "," C "]

    
asked by Efrainrodc 19.06.2017 в 20:31
source

2 answers

2

The simplest solution:

f :: [a] -> [[a]] -> [(a,a)]
f xs ys = [(x,y) | (x,zs) <- zip xs ys, y <- zs]
    
answered by 20.06.2017 / 02:11
source
1
f2 :: a -> [a] ->[(a,a)]
f2 a [] = []
f2 a (h:t) = (a,h):(f2 a t)

f :: [a] -> [[a]] -> [(a,a)]
f [] [] = []
f (h1:t1) (h2:t2) = (f2 h1 h2) ++ (f t1 t2)

This is a basic solution, which does not consider for example that the lists are of different sizes, but it should help you to orient and improve it.

    
answered by 19.06.2017 в 20:46