Newsgroups: comp.lang.scheme
Path: cantaloupe.srv.cs.cmu.edu!rochester!udel!news.mathworks.com!newsfeed.internetmci.com!tank.news.pipex.net!pipex!swrinde!elroy.jpl.nasa.gov!lll-winken.llnl.gov!uop!csus.edu!netcom.com!dkuhlman
From: dkuhlman@netcom.com (G. David Kuhlman)
Subject: Re: Java imps not tail recursive
Message-ID: <dkuhlmanDCouuw.2tL@netcom.com>
Organization: NETCOM On-line Communication Services (408 261-4700 guest)
X-Newsreader: TIN [version 1.2 PL1]
References: <RAMSDELL.95Jul30183001@linus.linus.mitre.org> <RAMSDELL.95Jul31063944@linus.linus.mitre.org>
Date: Wed, 2 Aug 1995 14:56:55 GMT
Lines: 64
Sender: dkuhlman@netcom13.netcom.com

Are you saying that it is not possible to compile Scheme code 
to Java byte code in such a way that tail recursion is converted
to iteration?


John D. Ramsdell (ramsdell@linus.mitre.org) wrote:
: The purpose of my last note was to warn Schemers that the Java
: language specification fails to require that implementations of the
: language be tail recursive.  In fact, the bytecode interpreter that
: comes from Sun is not tail recursive.  Didn't we go through this
: before with Dylan?

: John

: Here is an example of an iterative process expressed by means of
: procedure calls.
: ---------------------------------------------------------------------
: OddEven.java:
: ---------------------------------------------------------------------
: class OddEven {
:   public static void main(String args[]) {
:     for (int i = 0; i < args.length; i++) {
:       int n = java.lang.Integer.parseInt(args[i]);
:       if (n >= 0)
: 	System.out.println(n + " is " + (even(n) ? "even" : "odd"));
:     }
:   }
:   static boolean even(int i) {
:     if (i == 0) return true;
:     else return odd(i - 1);
:   }
:   static boolean odd(int i) {
:     if (i == 0) return false;
:     else return even(i - 1);
:   }
: }
: ---------------------------------------------------------------------
: Had the Jave byte code compiler and virtual machine been tail
: recursive, the OddEven program would never overflow the stack.  Here
: is a transcript of what happens with the current implementation.
: ---------------------------------------------------------------------
: bash$ java OddEven 0 1 2 3
: 0 is even
: 1 is odd
: 2 is even
: 3 is odd
: bash$ java OddEven 9234913
: java.lang.StackOverflowException
: 	at OddEven.odd(OddEven.java:15)
: 	at OddEven.even(OddEven.java:11)
: 	at OddEven.odd(OddEven.java:15)
: 	at OddEven.even(OddEven.java:11)
: 	at OddEven.odd(OddEven.java:15)
: 	at OddEven.even(OddEven.java:11)
: 	at OddEven.odd(OddEven.java:15)
: 	at OddEven.even(OddEven.java:11)
: 	at OddEven.odd(OddEven.java:15)
: 	at OddEven.even(OddEven.java:11)
: bash$ 
-- 
----------------------
Dave Kuhlman
Reify, Redwood City, CA
Internet:   dkuhlman@netcom.com
----------------------
