title: Nested foreach in NetLogo
body: <p>I am trying to calculate the Gini coefficient of a set of numbers. The Gini coefficient is half the mean absolute difference. That is, for every possible pair of numbers in the list, I need to take their absolute difference and add these differences together (and some other stuff). This is my code</p>
  
  <pre><code>to-report calc-Gini [list-Values]
    let sumdiff 0
    foreach list-Values
    [ foreach list-Values
      [ set sumdiff sumdiff + abs ( ?1 - ?2 )
      ]
    ]
    report 0.5 * sumdiff / (mean list-Values * (length list-Values) ^ 2)
  end
  </code></pre>
  
  <p>When I test it (eg <code>show calc-Gini (list 1 2 3)</code>) I get an error "task expected 2 inputs, but only got 1" on the second <code>foreach</code>.</p>
  
  <p>I think the problem is that NetLogo wants to run through the foreach loops simultaneously. So if the list length is N, then it creates only N pairs (that is, first item in list1 and first item in list2, then the second item in each list etc) which is where the requirement for equal length lists comes from. But I need it to work with the N^2 pairs obtained by crossing the lists.</p>
  
  <p>How can I make the nested <code>foreach</code> do what I want and/or is some other primitive more appropriate?</p>