# nest non pure function

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}}.

• 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. Commented Oct 26, 2017 at 9:42
• actually, I used N' and it's results if fine. just can't be nest. Commented Oct 26, 2017 at 9:55

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.

• 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! Commented Oct 26, 2017 at 12:50
• @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. Commented Oct 26, 2017 at 13:02
• I'm sorry, I'm a green hands and couldn't find the accept button🙃. But, I suppose the system will handle it. Commented Oct 26, 2017 at 17:18
• 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. Commented Oct 26, 2017 at 20:52
• Thanks for the accept! :-) Commented Nov 4, 2017 at 9:43