tags:

views:

161

answers:

1
  clock_t a = 0;
  clock_t b = 100;
  while(a<b)
  {
      // get current time
      a = clock();
  }

It seems that it is an infinite loop. Can the while loop be broken out of?

A: 

You have an infinitely recursive call to clock() (which I'm guessing is the code you posted).

Each time the method is called, the values are initialized. a will always be less than b and the method will be called again.

Justin Niessner
I would imagine it would blow the stack within seconds if it was truly recursion.
Amardeep
@Amardeep: Recursion conceptually includes all kinds of loops.
Potatoswatter
where's the recursion? I don't see that whole snipped inside a clock() function.
Maverick
@Maverick: `while` loop = recursion. Just not a recursive function.
Potatoswatter
@Potatoswatter, so it's just an infinite loop then. I don't see any stack building and thus overflow :)
Maverick
When I went to school recursion only took place if a function called itself. Loops were loops and stacks were not infinite. I guess times have changed.
Amardeep
@Mav: who said anything about that? See my reply to amar.
Potatoswatter
@Amar: The definition of recursion has never changed. http://en.wikipedia.org/wiki/Recursion_%28computer_science%29: Most high-level computer programming languages support recursion by allowing a function to call itself within the program text. Imperative languages define looping constructs like “while” and “for” loops that are used to perform repetitive actions.
Potatoswatter
@Potatoswatter: You're definitely confusing "looping", "iteration", and "recursion". `while` and `for` are looping using iterative construct. `recursion` != `looping`, recursion only happens when a function `looping` by calling itself.
Lie Ryan
From the wikipedia page you linked: "Recursion in computer science is a method where the solution to a problem **depends on solutions to smaller instances** of the same problem." The part of the text you're quoting are definitely red herring, I'll edit that page.
Lie Ryan
@Lie: Please don't, I'd bet they deal with this misconception a lot. The body of a loop corresponds to the body of a recursive function with an argument for every variable used inside the loop. For any kind of semantic analysis, such as done by the compiler, there is no difference. Mathematically they are the same thing.
Potatoswatter
@Potatoswatter: Recursion has a definite meaning both in computer science and mathematics, and it's not iteration. Mathematically, they *solve the same set of problems*, but they are **not** the same. For example, the recursive definition of arithmetic sequence is: `f(1) = 1; f(x) = x + f(x-1)`. The iterative definition is `f(x) = sigma[n=1..x] n` (sigma is the mathematical notation for sum).
Lie Ryan
@Lie: The computer science definition of recursion has a mathematical meaning, and loops are recursive structures. Basic blocks in imperative languages are similar to functions in functional languages, and once the code has been interpreted, they look much the same. Recursion means an edge pointing upwards in the control flow graph, from a node or its child back to itself. Iteration is the particular case where one variable changes in a simple way from one execution of the body to the next, and is used in the termination condition.
Potatoswatter
@Potatoswatter: you have just shown that they *solves the same set of problems*, what you have not shown is that they refer to the same approach of solving a problem.
Lie Ryan
@Lie: I'm sorry I haven't proven it to your satisfaction. Don't be surprised if you hear other people refer to looping as a form of recursion.
Potatoswatter
@Potatoswatter: You can't possibly prove it, nor can anyone. Because they're referring to two different approaches of solving a problem. By definition, they are different. It's easy to misconclude, because they can solve exactly the same set of problems, therefore they are the same. What you're trying to claim is similar to concluding that Intel and AMD is the same company, because they solves exactly the same problem.
Lie Ryan
@Potatoswatter: That iteration and recursion is intertranslatable does not matter. That they have to be translated, does matter. While I have seen lots of people (correctly) claiming that iteration and recursion solves the same set of problems, you are actually the first person I have seen that concludes that iteration is recursion, and recursion is iteration.
Lie Ryan
@Lie: Recursion is not a form of iteration. All iteration may be trivially transformed to tail-recursion. When a loop loops, it is passing a set of implicit arguments back to itself. When I say they are the same concept, I mean they are the same concept. Not the same computability class, the exact same thing. I'm sorry you haven't been exposed to functional programming, compilers, etc. but it sounds like you've finished school and you're done learning, so bye.
Potatoswatter
@Potatoswatter: All iteration can be trivially transformed to tail-recursion, and all recursion can be trivially transformed to iteration using a stack. That does not mean they can be transformed into the other form without any transformation. I'm sorry to hear you have brought your misconception out from school for so long.
Lie Ryan