문제

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.

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top