Search code examples
crecursionschemesicp

Why does C preserves both goto and while, involving tail recursion


Tail recursion performs the same effect as goto. Or to say, in lang C, while and goto substitute the role of tail recursion in Scheme. What made designers decided to preserve both goto and while implementations in C? As I think people hardly ever use 'goto' in actual situations.

If goto and while are to make up the fault that C cannot implement iteration by writing in a recursive way, are they in same special construction? - to avoid normal recursion. (just as 'cond' differs 'if' in Scheme) Is this problem related to the reason why they preserve both words?


Solution

  • Tail recursion is not a replacement for goto. Tail calls are, if you also have first-class functions (which C doesn't have either).

    What made designers decided to preserve both goto and while implements in C?

    Without while (and for, to a slightly lesser extent), you can't do structured programming properly - goto is rarely the best way to express control flow. It's still useful to have goto (at least in the context of C) as it can express some patterns for which there's no built-in control flow constructor more clearly and efficiently. For example, it yields easier and reliable clean up on function exit than repeating the clean up at every possible exit point.

    If C had scheme-like macros, perhaps it could do fine with just one primitive, goto, with all loop constructs built on top of goto as macros. But C doesn't have such macros, for good reasons (and some not so good reasons).