Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Recursion is not a looping construct.

by Adam (Vicar)
on Jun 10, 2000 at 22:31 UTC ( [id://17520]=note: print w/replies, xml ) Need Help??

in reply to RE (6) Adam: Answer: How can I add all the numbers in an array with out doing a foreach loop?
in thread How can I add all the numbers in an array with out doing a foreach loop?

Sorry Nuance, Recursion is NOT a loop.
Recursion works by building up the stack, delaying evaluation until the end. A loop evaluates as it goes. There is a big difference between the two.

Think about it this way: The recursive method that I posted says to add the first number to the sum of the other numbers. That can't be done until the sum of the other numbers is known, so it puts the number on the stack and trys to sum the other numbers by the same method. It does this, pushing the list onto a stack, until there is only one number left, then it sums the stack. This is not a loop. Its not even close.

Replies are listed 'Best First'.
RE: Recursion is not a looping construct.
by lhoward (Vicar) on Jun 11, 2000 at 02:28 UTC
    Some programming languages optomize tail-recursion down to a loop (to prevent the procedure call/stack overhead). So at least I would say that tail-recursion is a looping construct (or at least very close to one). Also some languages (ML, Prolog) have no standard looping constructs (while, for, etc..) so all looping in those languages is implemented with recursion. So in at least those languages recursion is a looping construct.

    The call is harder to make in Perl, since I don't believe it optomizes tail-recursion down to a loop, and it does have normal looping operators. There are cases where recursion is clearly not a looping construct, but I must say that in some instances recursion clearly is a looping construct.

RE: Recursion is not a looping construct.
by KM (Priest) on Jun 11, 2000 at 05:49 UTC
    Well, I disagree with you here. And, so do the people I asked for opinions. Recursion is a looping construct. Even your explination is a loop. You can not push things onto a stack and then evaluate what is on that stack incrementally without a loop. No need to really continue this, but after trying to find anyone else I know to have the same opinion as you, I stand by everyone elses answers that this can't be done without looping, and that recursion is indeed a looping construct. Just take a pencil and paper and draw out a flow of the snippet you gave, you will see the obvious loop.


Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://17520]
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2024-07-24 08:43 GMT
Find Nodes?
    Voting Booth?

    No recent polls found

    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.