Question

I am writing a function that takes two lists and needs to make a new list like this:

(a b) (c d e f) --> ((a b c) (a b d) (a b e) (a b f))

i.e., each element of the second list is added as an element to the first list, one for each element of the second list.

I can't figure out how to do this. I have tried variations of:

(map list (list1) (list2))

with no success.

Any advice is appreciated.

Was it helpful?

Solution

Using map with multiple arguments will iterate the lists in parallel, which is not what you want.

What you want is basically to prepend the first list to each of the elements in the second list. So you want something like:

(map (lambda (x) (append list1 (list x))) list2)

Note that appending to the end of a list is an O(n) operation, so if you can restructure your algorithm so that you can prepend each item at the beginning of the list, that would be better.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top