Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to use while loop in Haskell.
Code example would be great.

how to use while loop in Haskell.
Code example would be great.


how to use while loop in Haskell.
Code example would be great.


What I have tried:

how to use while loop in Haskell.
Code example would be great.


how to use while loop in Haskell.
Code example would be great.


how to use while loop in Haskell.
Code example would be great.
Posted
Updated 6-Mar-17 19:53pm

1 solution

I don't know Haskell, but there is one thing that should still be valid for a functional language: Iteration (loops) can always be replaced by an equivalent recursion or vice versa.

The least confusing definition of recursion[^] is a function that calls itself. Each call represents an iteration of the original loop. Endless recursion does not work (it usually ends in a stack overflow), therefore there must be some condition that ends the recursion and simply returns.

For a 'while type' recursion, you would have to test your condition in order to decide wether to execute your function or to return. This is C-like pseudocode:

C
ReturnType RecursiveFunction(SomeParameters)
{
    if(Condition == true)
    {
        return;
    }
    else
    {
        // Do whatever your loop should have done,
        // possibly modifying the parameters that have been passed.

        // recursive call to your function
        return RecursiveFunction(ModifiedParameters);
    }
}


Perhaps this link can help:
Recursion - Learn You a Haskell for Great Good![^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900