Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!zaphod.mps.ohio-state.edu!rpi!clarkson!grape.ecs.clarkson.edu!maguire From: maguire@sun.soe.clarkson.edu (Bill Maguire) Newsgroups: comp.lang.scheme Subject: Re: Scheme recursive procedures Message-ID: Date: 1 Feb 91 16:48:32 GMT References: <11739@darkstar.ucsc.edu> <5834@idunno.Princeton.EDU> Sender: @grape.ecs.clarkson.edu Organization: Clarkson University, Potsdam NY Lines: 52 In-Reply-To: markv@fourier.Princeton.EDU's message of 31 Jan 91 15:57:05 GMT >In article <11739@darkstar.ucsc.edu> gforce@ucscb.UCSC.EDU (60766000) writes: >> >>Can one of you scheme hackers out there help me with a prog. problem ? >>the version of scheme at my school doesn't have the function "reverse" >>I am trying to write one but so far have only come up with one that is >>recursive but needs to take in two arguments.. CAn someone show me a way >>to do it with only one ?.. >> >>Thanx In article <5834@idunno.Princeton.EDU> markv@fourier.Princeton.EDU (Mark VandeWettering) writes: >Sigh! Someone should work harder to understand what is going on. If you >can't code reverse, then you can't code LOTS. >But, in true spirit of a spoiler.... >Try any one of the following (done off the top of my head): > > (define (reverse l) > (if (null? l) '() > (append (reverse (cdr l)) (car l)))) > > (define (reverse-tr l) > (define (rloop l r) > (if (null? l) > r > (rloop (cdr l) (cons (car l) r)))) > (rloop l '())) > >By some strange coincidence, we also have gotten rid of the append problem. >This will execute in all reasonable Scheme implementations with a constant >amount of stack, since it is fully tail recursive. >Mark VandeWettering >markv@acm.princeton.edu If you really want to hack though, why not: (define reverse (lambda (l) (call-with-current-continuation (lambda (return) (let ((r nil) (loop 'any-value)) (call-with-current-continuation (lambda (c) (set! loop c))) (if (null? l) (return r)) (set! x (cons (car l) r)) (set! l (cdr l)) (loop 'any-value)))))) It also uses constant stack space... -Bill Maguire