Search code examples
functional-programmingmiranda

Miranda while- and for-loops


I'm looking for a way to do while-loops or for-loops in Miranda.

I'm trying to do something like

while(blablanotfinished)
{
if(a=true)blabla
else blabla
}

Solution

  • Miranda doesn't have while- or for-loops (which wouldn't make much sense without mutable state anyway). In most cases you can use higher order functions instead. In cases where there is no higher-order function which does what you need, you can use recursion.

    For example if you have the following while-loop in an imperative language:

    f(start) {
        x = start
        while( !finished(x) ) {
            x = next(x)  
        }
        return x
    }
    

    You would express it recursively in Miranda like this:

    f x = if finished x then x else f (next x)