From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=0.2 required=5.0 tests=BAYES_00,INVALID_MSGID, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,f3168034ad0ab067 X-Google-Attributes: gid103376,public From: Larry Coon Subject: Re: Recursion Date: 1997/10/06 Message-ID: <34394AFE.2942@fs2.assist.uci.edu>#1/1 X-Deja-AN: 278933640 References: <34392AFD.280C@newtimes.com> Organization: University of California Reply-To: larry@fs2.assist.uci.edu Newsgroups: comp.lang.ada Date: 1997-10-06T00:00:00+00:00 List-Id: Dirien B. Perez wrote: > Hi I am having some problems with recursion I have the iterative > version of a function that will calculate the sum of the elements is a > linked list: > > function sum(list:cell_ptr) return integer is > local:cell_ptr:=list; > s:integer:=0; > begin > while local /= null loop > s:=s+local.value; > local:=local.next > end loop; > return s; > end sum; > I need help creating the recursion version. I don't want to do your homework for you (I'm assuming that's what this is for), but it should be something like: function sum (current_cell: cell_ptr) return int is if current_cell.next = null then return 1; else return sum (current_cell.next) + 1; end if; end sum; I'll leave it to you to modify this to correctly distinguish an empty linked list from one with a single item. I'll also leave it to you to extrapolate from this problem to your other problem. Larry Coon University of California larry@fs2.assist.uci.edu and lmcoon@home.com