Mutual optimization of tail recursion does not work in D

Dennis Ritchie via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Mar 31 04:51:46 PDT 2015


Hi,

This code does not work:

import std.stdio;

bool odd(int n);
bool even(int n);

bool even(int n) {
	if (n == 0)
		return true;
	else
		return odd(n - 1);
}

bool odd(int n) {
	if (n == 0)
		return false;
	else
		return even(n - 1);
}

void main() {

	bool r = odd(655370);
	
	writeln(r);
}

http://ideone.com/GSNMxl
	
This code works completely:

#include <cstdio>

bool odd(int n);
bool even(int n);

bool even(int n) {
	if (n == 0)
		return true;
	else
		return odd(n - 1);
}

bool odd(int n) {
	if (n == 0)
		return false;
	else
		return even(n - 1);
}

int main() {
	
	bool r = odd(655370);
	
	printf("%d\n", r); // prints 0
	
	return 0;
}

http://ideone.com/TT48zT

Why D does not work?


More information about the Digitalmars-d-learn mailing list