Re: Tail recursion

"Daniel C. Wang" <danwang+news@cs.princeton.edu>
14 Aug 2000 17:30:32 -0400

          From comp.compilers

Related articles
Tail recursion strohm@airmail.net (2000-08-10)
Re: Tail recursion pfaffben@msu.edu (Ben Pfaff) (2000-08-13)
Re: Tail recursion danwang+news@cs.princeton.edu (Daniel C. Wang) (2000-08-14)
Re: Tail recursion toon@moene.indiv.nluug.nl (Toon Moene) (2000-08-14)
Re: Tail recursion fjh@cs.mu.OZ.AU (2000-08-21)
Re: Tail recursion Wilco.Dijkstra@arm.com (Wilco Dijkstra) (2000-08-21)
Re: Tail recursion mrs@kithrup.com (2000-08-21)
Re: Tail recursion ian0kerr@my-deja.com (2000-09-08)
Tail recursion Alexey.Mikhailov@gmail.com (jjb) (2006-11-04)
[4 later articles]
| List of all articles for this month |
From: "Daniel C. Wang" <danwang+news@cs.princeton.edu>
Newsgroups: comp.compilers
Date: 14 Aug 2000 17:30:32 -0400
Organization: Princeton University
References: 00-08-054 00-08-071
Keywords: optimize

Ben Pfaff <pfaffben@msu.edu> writes:


> strohm@airmail.net (John R. Strohm) writes:
>
> > Do any of the commonly-available compilers for "mainstream" languages
> > do tail recursion optimization? In particular, any C, c++, or Ada
> > compilers?
>
> The GCC compiler suite does tail recursion optimization at least
> for C and C++ and probably for its other languages as well.


But only "intra-procedurally"


#include<stdio.h>
#include<stdlib.h>
#include<time.h>


static int n;
static clock_t start;
void even(void);
void odd(void);


void even() {
    if(n == 0) {
          exit(0);
    } else {
        n--;
        odd();
    }
}


void odd() {
    if(n == 0) {
        exit(0);
    } else {
        n--;
        even();
    }
}


Last I checked gcc didn't turn the above program into


even:
  if (n == 0) {
        exit(0)
    } else {
        n--;
        goto odd;
    }


odd:
    if (n == 0) {
      exit(0);
    } else {
      n--
      goto even;
    }




which a smart Scheme and ML compiler would do...


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.