Calculate the Cartesian product with functional programming

1

I am writing a function in Python that gets two lists and returns a list of tuples with the Cartesian product of the input function. I can not use any function from the Python library.

Ex:

print(cartesiano([1,2],[11,22]))
# [(1, 11), (1,22), (2,11), (2, 22)]

My program is printing this: [(1, 11), (2, 22)] . I can not fix the recursion.

def cartesiano(lista1,lista2):

def aux(l1,l2):
    if l1==[] and l2==[]:
       return []

    return [(l1[0],l2[0])] +aux(l1[1:],l2[1:])       

return aux(lista1, lista2) 

I can not use repetition loops, as it is much more intuitive. It's a matter of functional programming, no ties, etc ...

    
asked by anonymous 01.04.2018 / 01:23

1 answer

3

In an approach that reminds me a little more of functionality and logic, we can think of list heads and list body. The old functions CAR and CDR of

03.04.2018 / 00:31