AXForum  
Вернуться   AXForum > Microsoft Dynamics AX > DAX Blogs
All
Забыли пароль?
Зарегистрироваться Правила Справка Пользователи Сообщения за день Поиск

 
 
Опции темы Поиск в этой теме Опции просмотра
Старый 28.10.2006, 16:40   #1  
Blog bot is offline
Blog bot
Участник
 
25,626 / 848 (80) +++++++
Регистрация: 28.10.2006
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
 

Похожие темы
Тема Автор Раздел Ответов Посл. сообщение
Dynamics AX: Managing Your Supply Chain Using Microsoft Dynamics AX 2009 - Book Review Blog bot DAX Blogs 0 31.03.2009 23:06
axStart: Microsoft Dynamics AX 2009 Hot Topics Web Seminar Series Blog bot DAX Blogs 0 06.08.2008 12:05
Inside Dynamics AX 4.0: Usage Scenarios Blog bot DAX Blogs 0 04.10.2007 05:15
Сергей Герасимов: Что нового в Microsoft Dynamics AX 4.0 Blog bot DAX Blogs 0 16.01.2007 11:00
Dynamics AX Geek: Axapta & precision Blog bot DAX Blogs 0 28.10.2006 16:40

Ваши права в разделе
Вы не можете создавать новые темы
Вы не можете отвечать в темах
Вы не можете прикреплять вложения
Вы не можете редактировать свои сообщения

BB коды Вкл.
Смайлы Вкл.
[IMG] код Вкл.
HTML код Выкл.
Быстрый переход

Рейтинг@Mail.ru
Часовой пояс GMT +3, время: 02:27.