Hi Leo, Very similar - it's just that the elements being compared change - so here using cadr rather than car to get the second elements in the lists. C=foreach(mapcar elem1 A list( elem1 car(exists(elem2 B cadr (elem1)== cadr (elem2))) ) ) will do this. A brief coverage of mapping functions. The various mapping functions, map, mapc, mapcar, mapcan, maplist, mapcon each accept a function of one or more arguments, plus as many lists as there are arguments to the function passed in as the first argument to the mapping function. The mapc, mapcar, and mapcan functions work on successive elements of the lists, whereas map, maplist and mapcan are passed successive lists. map and mapc return the original list (actually the first list), mapcar and maplist return a list constructed from the return values of the function called, and mapcan and mapcon expect the function called to return a list, and they then destructively concatenate all the returned lists together. Some examples: a='(1 2 3 4) b=mapc('println a) prints: 1 2 3 4 and b is set to (1 2 3 4) b=map('println a) prints: (1 2 3 4) (2 3 4) (3 4) (4) return value is the same as with mapc. b=mapcar('minus a) returns (-1 -2 -3 -4) - as you can see, it applied the minus function to each element in the list and returned that. Anyway, the foreach form is a simple shorthand to avoid you having to define a function. If you don't provide the mapping function as the first argument, it's doing a mapc. If you do specify it, it behaves as with that mapping function. So: b=foreach(mapcar elem a elem**2) is the same as doing: procedure(myfunc(val) val**2 ) b=mapcar('myfunc a) The last thing in the body of the foreach is the return value used to assemble the list. These both return (1 4 9 16). So, to describe how the code above works, it loops over the first list (A), and then uses exists() to find if the first (or second) element of the sublist it is on matches the first (or second) element of anything in list B, and then builds a list of the A sublist and the found B sublist, and then assembles a new list based on these newly created pairs. I hope that helps? Regards, Andrew
↧