0
$\begingroup$

I have a problem to nest a non pure function.The function f has two arguments, i and j, it can come out four results {i + 2, j - 1}, {i + 1, j}, {i, j + 1}, {i - 1, j + 2} which I use If conditional to programme it. But now, what I want to do it's to nest this function, like I input i = 1, j = 0, then the four results will follow the F subsequently. it's like if I want nest it 3 times, then it should have 12 results. but the error show that "part specification [][]is longer than depth of the object".

Update

f = Function[{i, j}, 
     If[(i > 0) && (j > 0), 
      {{i + 2, j - 1}, {i + 1, j}, {i, j + 1}, {i - 1, j + 2}}, 
      If[j == 0, 
       {{i + 1, j}, {i, j + 1}, {i - 1, j + 2}}, 
       {{i + 2, j - 1}, {i + 1, j}, {i, j + 1}}
      ]
     ]
    ];

This the function I use. I hope I can nest this function several times and get a long list results. The one step work for example put i = 1, j = 1, I get {{3, 0}, {2, 1}, {1, 2}, {0, 3}}.

$\endgroup$
  • $\begingroup$ If your function name is N then there might be a problem. It's best not to use capitals as they conflict with built-in functions. $\endgroup$ – aardvark2012 Oct 26 '17 at 9:42
  • $\begingroup$ actually, I used N' and it's results if fine. just can't be nest. $\endgroup$ – Ivy Gao Oct 26 '17 at 9:55
2
$\begingroup$

This should work. Define your function:

f[i_, j_] := 
  If[i > 0 && 
    j > 0, {{i + 2, j - 1}, {i + 1, j}, {i, j + 1}, {i - 1, j + 2}},
   If[j == 0,
    {{i + 1, j}, {i, j + 1}, {i - 1, j + 2}},
    {{i + 2, j - 1}, {i + 1, j}, {i, j + 1}}
    ]
   ];

Then

NestList[Flatten[f @@@ #, 1] &, {{1, 1}}, 3]

(* {{{1, 1}}, 
    {{3, 0}, {2, 1}, {1, 2}, {0, 3}}, 
    {{4, 0}, {3, 1}, {2, 2}, {4, 0}, {3, 1}, {2, 2}, {1, 3}, 
     {3, 1}, {2, 2}, {1, 3}, {0, 4}, {2, 2}, {1, 3}, {0, 4}}, 
    {{5, 0}, {4, 1}, {3, 2}, {5, 0}, {4, 1}, {3, 2}, {2, 3}, {4, 1}, {3, 2}, {2, 3}, 
     {1, 4}, {5, 0}, {4, 1}, {3, 2}, {5, 0}, {4, 1}, {3, 2}, {2, 3}, {4, 1}, {3, 2}, 
     {2, 3}, {1, 4}, {3, 2}, {2, 3}, {1, 4}, {0, 5}, {5, 0}, {4, 1}, {3, 2}, {2, 3}, 
     {4, 1}, {3, 2}, {2, 3}, {1, 4}, {3, 2}, {2, 3}, {1, 4}, {0, 5}, {2, 3}, {1, 4}, 
     {0, 5}, {4, 1}, {3, 2}, {2, 3}, {1, 4}, {3, 2}, {2, 3}, {1, 4}, {0, 5}, {2, 3}, 
     {1, 4}, {0, 5}}
   } *)

The tricky part is that, the depth of the expression increases with each iteration, making it hard to use Map or Apply. That's why Flatten is in there, and also why the initial expression {{1, 1}} has an extra level of List, {...} -- for consistency so you don't need to try to Apply f at a different level in each iteration.

I'm assuming it's okay that the results get Flattened since you made no mention of it in the question. There is, of course, structure being lost. But without Flattening, the expression you're left with after 3 iterations has Depth 6 and not much to recommend using it.

$\endgroup$
  • $\begingroup$ you have solved the problem perfectly, I don't know why they won't suggest to say thanks in the comments. but whatever, I must thank you! $\endgroup$ – Ivy Gao Oct 26 '17 at 12:50
  • $\begingroup$ @IvyGao No problem at all! The best way to say thanks is to upvote any useful posts, and click on the tick to accept an answer if it solves your problem successfully. $\endgroup$ – aardvark2012 Oct 26 '17 at 13:02
  • $\begingroup$ I'm sorry, I'm a green hands and couldn't find the accept button🙃. But, I suppose the system will handle it. $\endgroup$ – Ivy Gao Oct 26 '17 at 17:18
  • $\begingroup$ Sadly, no. The system won't "handle it". It's It's a greyed out tick to the left of this answer, under the number and voting arrows. You can look here or take the tour for more details. $\endgroup$ – aardvark2012 Oct 26 '17 at 20:52
  • $\begingroup$ Thanks for the accept! :-) $\endgroup$ – aardvark2012 Nov 4 '17 at 9:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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