0
$\begingroup$

This question already has an answer here:

It looks like the following code should work, and allow it spits out error messages saying I have too many arguments, the code spit out after the error message looks like exactly what I want to calculate, but it won't give the answers. Completely stumped here...

list[b_, c_] := Table[{a, b, c}, {a, 0, c - 1}];
list2 = Tuples[Table[list[Prime[8], c], {c, 5, 7}]];

list3[a_, b_, c_] := Union[
  Range[a, b, Prime[c]],
  Range[a + 2, b, Prime[c]]]

list4 = Union @@@ Apply[list3, list2, {2}];
Apply[Differences, list4, 1]

Here's the output:

{Differences[0, 2, 11, 13, 15, 17, 19], 
 Differences[0, 1, 2, 3, 11, 13, 15, 18], 
 Differences[0, 2, 4, 11, 13, 15, 19], 
 Differences[0, 2, 3, 5, 11, 13, 15], 
 Differences[0, 2, 4, 6, 11, 13, 15], 
 Differences[0, 2, 5, 7, 11, 13, 15],...

Those are the differences I want. How do I get it to spit them out?

$\endgroup$

marked as duplicate by ciao, m_goldberg, Mr.Wizard list-manipulation May 26 '16 at 0:59

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • $\begingroup$ Why are you applying? This should be mapped... $\endgroup$ – ciao May 25 '16 at 22:48
  • $\begingroup$ @ciao Sorry, I'm new. What do you mean? $\endgroup$ – Elem-Teach-w-Bach-n-Math-Ed May 25 '16 at 22:49
  • $\begingroup$ Map[Differences, list4] or just Differences/@list4 $\endgroup$ – ciao May 25 '16 at 22:50
  • $\begingroup$ @ciao That did it! Would you post as an answer with an explanation of the difference :-) between apply and map? $\endgroup$ – Elem-Teach-w-Bach-n-Math-Ed May 25 '16 at 22:53
3
$\begingroup$

You want to Map here, not Apply:

Map[Differences, list4] or just Differences/@list4

Map takes the sub-lists of list4 as the argument to Differences for each, Applyreplaces the heads (List) on the sub-lists with Differences (so the elements of the list all become arguments to Differences), hence the error message: Differences expects a list (and optional remaining arguments up to 3 total arguments), but you are feeding it the list elements themselves.

The error message tells you this.

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.