comp.lang.ada
 help / color / mirror / Atom feed
From: Larry Coon <larry@fs2.assist.uci.edu>
Subject: Re: Recursion
Date: 1997/10/06
Date: 1997-10-06T00:00:00+00:00	[thread overview]
Message-ID: <34394AFE.2942@fs2.assist.uci.edu> (raw)
In-Reply-To: 34392AFD.280C@newtimes.com


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




  reply	other threads:[~1997-10-06  0:00 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1997-10-06  0:00 Recursion Dirien B. Perez
1997-10-06  0:00 ` Larry Coon [this message]
1997-10-09  0:00 ` Recursion Michael F Brenner
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox