[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Two versions of sum over node list by recursion--w

Subject: Re: Two versions of sum over node list by recursion--why and how does second one work?
From: David Carlisle <davidc@xxxxxxxxx>
Date: Tue, 5 Sep 2006 17:14:21 +0100
over stack
the second is the normal recursive definition of the sum of a list: the
sum function doesn't take a parameter, the result is just returned as
the result of the function.

sum of a list is defined by

sum (empty-list) = 0
sum(list)= 1st-item + sum(rest-of-list)


see, sum() here doesn't need an explict result parameter.
technically though this means that intermediate results get saved in the
processor's function call stack and so if you have too long a list you
run out of stack space.

Such functions (for some definition of "such") can always be written in
tail recursive form where instead the intermediate results are instead
accumulated in a parameter

sum(list)=sum2(list,0)
sum2(empty-list,total)=total
sum2(list,total)=sum2(rest-of-list,1st-item+total)

For Dimitre, it can also be written as a divide and conquer
sum(empty-list)=0
sum(item)=item
sum(list)=sum(even-position-items)+sum(odd-position-items)

David

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2011 All Rights Reserved.