|
28.10.2006, 16:40 | #1 |
Участник
|
Dynamics AX Geek: Recursion in Axapta
Источник: http://AxGeek.spaces.live.com/Blog/c...DB13!130.entry
============== Axapta can handle recursive method calls and you can see the standard application making use of it in some places, for example in \Classes\ReqTransFormExplosion\treeBuildNode. But just deep can you go before it bugs out? Let’s take a classic example of recursive programming: Towers of Hanoi. The Tower of Hanoi puzzle was invented by the French mathematician Edouard Lucas in 1883. We are given three pegs and a tower of n disks, initially stacked in increasing size on one of three pegs. The objective is to transfer the entire tower to one of the other pegs, moving only one disk at a time and never a larger one onto a smaller. The code to solve this puzzle is minimal, very intuitive – and recursive. static void TowersOfHanoi(Args _args) { void move(int _n,str _from, str _to, str _transfer) { if (_n > 1) { move(_n-1,_from,_transfer,_to); move(1,_from,_to,_transfer); move(_n-1,_transfer,_to,_from); } else print strfmt("%1 -> %2",_from, _to); } ; move(3,"Peg #1","Peg #3","Peg #2"); pause; } The parameters instruct the function to move 3 discs from peg #1 to peg #3 and use peg #2 as a buffer. The solution is to move n-1 discs to the buffer, the last (biggest) disc to the destination and then move the rest from the buffer to the destination. How do you move the rest from the buffer to the destination? Well, you move n-1 discs… you get the point. Back to original question: How far can you go before it crashes? The answer is 400. Try it out. Change _n from 3 to 400 and run the job. No problem. It will take a long time to finish, so unless you really need to know how to move 400 discs, I suggest you interrupt with Ctrl+Break :-). Now change _n to 401. It crashes almost instantly. Each call to move adds one level of recursion (if _n > 1), so that’s 399 + 1 for the initial call = 400. Btw: Don’t use info() for the output. You would run into a whole other limitation with the infolog. ============== Источник: http://AxGeek.spaces.live.com/Blog/c...DB13!130.entry |
|
|
Опции темы | Поиск в этой теме |
Опции просмотра | |
|