user299791
user299791

Reputation: 2070

find lists intersection in NetLogo

I have a list of n lists and want to find their intersection, like:

[[1 2 3 4][0 4][4 1]]
[4]

I have found a code here to find the intersection of two lists:

to-report intersect [a b]
   report (filter [ member? ? b ] a)
end

but I cannot figure out how I can extend this concept to multiple lists.

Upvotes: 2

Views: 341

Answers (1)

Nicolas Payette
Nicolas Payette

Reputation: 14972

Oh! This is a very nice use case for NetLogo's under-appreciated reduce function.

Given your intersect reporter above:

print reduce intersect [[1 2 3 4][0 4][4 1]]

Will give you:

[4]

reduce applies intersect to the first and second sub-lists, and then it applies intersect to the result of that and the third sub-list, and so on for as many sub-lists as you have in your master list...

Upvotes: 2

Related Questions