
Hello everyone In C you can't implement main loop with recursion like void mainLoop() { doSomething(); mainLoop(); } because without optimisations stack will overflow. In haskell it's common to write mainLoop = doSomething >> mainLoop, and it doesn't leak memory because of haskell's evaluation model. Does memory leak or argument stack overflow happen in this case? mainLoop = doSomething >> mainLoop >> exit ExitSuccess What about this case? mainLoopModeA = do doSomething when condition mainLoopModeB mainLoopModeA mainLoopModeB = do doSomethingElse when anotherCondition mainLoopModeA mainLoopModeB or this case? mainLoopModeA = do doSomething if condition then mainLoopModeB else mainLoopModeA mainLoopModeB = do doSomethingElse if anotherCondition then mainLoopModeA else mainLoopModeB -- Nikita Fufaev

Any mutually recursive set of functions calling in tail position should be
fine - all your examples are ok.
On Tue, Jun 19, 2018, 2:37 PM Никита Фуфаев
Hello everyone
In C you can't implement main loop with recursion like void mainLoop() { doSomething(); mainLoop(); } because without optimisations stack will overflow. In haskell it's common to write mainLoop = doSomething >> mainLoop, and it doesn't leak memory because of haskell's evaluation model. Does memory leak or argument stack overflow happen in this case? mainLoop = doSomething >> mainLoop >> exit ExitSuccess What about this case? mainLoopModeA = do doSomething when condition mainLoopModeB mainLoopModeA mainLoopModeB = do doSomethingElse when anotherCondition mainLoopModeA mainLoopModeB
or this case? mainLoopModeA = do doSomething if condition then mainLoopModeB else mainLoopModeA mainLoopModeB = do doSomethingElse if anotherCondition then mainLoopModeA else mainLoopModeB
-- Nikita Fufaev _______________________________________________ Haskell-Cafe mailing list To (un)subscribe, modify options or view archives go to: http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe Only members subscribed via the mailman list are allowed to post.

Actually, only the second set is tail recursive. The first set could be
trouble.
On Tue, Jun 19, 2018, 8:03 PM Mark Wotton
Any mutually recursive set of functions calling in tail position should be fine - all your examples are ok.
On Tue, Jun 19, 2018, 2:37 PM Никита Фуфаев
wrote: Hello everyone
In C you can't implement main loop with recursion like void mainLoop() { doSomething(); mainLoop(); } because without optimisations stack will overflow. In haskell it's common to write mainLoop = doSomething >> mainLoop, and it doesn't leak memory because of haskell's evaluation model. Does memory leak or argument stack overflow happen in this case? mainLoop = doSomething >> mainLoop >> exit ExitSuccess What about this case? mainLoopModeA = do doSomething when condition mainLoopModeB mainLoopModeA mainLoopModeB = do doSomethingElse when anotherCondition mainLoopModeA mainLoopModeB
or this case? mainLoopModeA = do doSomething if condition then mainLoopModeB else mainLoopModeA mainLoopModeB = do doSomethingElse if anotherCondition then mainLoopModeA else mainLoopModeB
-- Nikita Fufaev _______________________________________________ Haskell-Cafe mailing list To (un)subscribe, modify options or view archives go to: http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe Only members subscribed via the mailman list are allowed to post.
_______________________________________________ Haskell-Cafe mailing list To (un)subscribe, modify options or view archives go to: http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe Only members subscribed via the mailman list are allowed to post.

Oops, yes, you are right - if it keeps switching back and forth the stack
will build up. Apologies, written while wrangling a toddler.
On Tue, Jun 19, 2018, 8:06 PM David Feuer
Actually, only the second set is tail recursive. The first set could be trouble.
On Tue, Jun 19, 2018, 8:03 PM Mark Wotton
wrote: Any mutually recursive set of functions calling in tail position should be fine - all your examples are ok.
On Tue, Jun 19, 2018, 2:37 PM Никита Фуфаев
wrote: Hello everyone
In C you can't implement main loop with recursion like void mainLoop() { doSomething(); mainLoop(); } because without optimisations stack will overflow. In haskell it's common to write mainLoop = doSomething >> mainLoop, and it doesn't leak memory because of haskell's evaluation model. Does memory leak or argument stack overflow happen in this case? mainLoop = doSomething >> mainLoop >> exit ExitSuccess What about this case? mainLoopModeA = do doSomething when condition mainLoopModeB mainLoopModeA mainLoopModeB = do doSomethingElse when anotherCondition mainLoopModeA mainLoopModeB
or this case? mainLoopModeA = do doSomething if condition then mainLoopModeB else mainLoopModeA mainLoopModeB = do doSomethingElse if anotherCondition then mainLoopModeA else mainLoopModeB
-- Nikita Fufaev _______________________________________________ Haskell-Cafe mailing list To (un)subscribe, modify options or view archives go to: http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe Only members subscribed via the mailman list are allowed to post.
_______________________________________________ Haskell-Cafe mailing list To (un)subscribe, modify options or view archives go to: http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe Only members subscribed via the mailman list are allowed to post.

Have you profiled it? I wouldn't worry about recursion until I've seen the problem. I believe GHC defaults to infinite stack size. Have you tried using hp2ps and/or looking at the code via threadscope? On 06/19/2018 01:36 PM, Никита Фуфаев wrote:
Hello everyone
In C you can't implement main loop with recursion like void mainLoop() { doSomething(); mainLoop(); } because without optimisations stack will overflow. In haskell it's common to write mainLoop = doSomething >> mainLoop, and it doesn't leak memory because of haskell's evaluation model. Does memory leak or argument stack overflow happen in this case? mainLoop = doSomething >> mainLoop >> exit ExitSuccess What about this case? mainLoopModeA = do doSomething when condition mainLoopModeB mainLoopModeA mainLoopModeB = do doSomethingElse when anotherCondition mainLoopModeA mainLoopModeB
or this case? mainLoopModeA = do doSomething if condition then mainLoopModeB else mainLoopModeA mainLoopModeB = do doSomethingElse if anotherCondition then mainLoopModeA else mainLoopModeB
-- Nikita Fufaev
_______________________________________________ Haskell-Cafe mailing list To (un)subscribe, modify options or view archives go to: http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe Only members subscribed via the mailman list are allowed to post.
participants (4)
-
David Feuer
-
Mark Wotton
-
Vanessa McHale
-
Никита Фуфаев