This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: result = node1 * node2 and then get total of all the result from whole document at the end


> I am having hard
> time adding the product of rate and quantity into a variable and then
> increment it and print it at the end of document.

http://www.dpawson.co.uk/xsl/sect2/N5121.html#d154e138

Look at the link "Quantity times price".


The corresponding functional solution in Haskell is:

sumOfProducts :: Num a => [[a]] -> a
sumOfProducts = sum . (map product)

where:

 sum :: (Num a) => [a] -> a
 sum = foldl add 0

and

 product :: (Num a) => [a] -> a
 product = foldl multiply 1


sumOfProducts() takes a list of lists of numbers, first "map" produces a new list
whose elements are the products of the numbers contained in every element (which is
itself a list of numbers), then finally "sum" sums the (products) elements of this
new list.

For example:

sumProducts [[1,2,3],[3,4]] evaluates to 18 (sum [1*2*3, 3*4] 
= sum [6, 12] = 6 + 12 = 18).

As can be seen this is much more powerful function, allowing to sum not only
products of pairs, but products of lists of numbers with varying length.

The XSLT implementation of this functional solution is straightforward and left as
an exercise for the interested readers.
  

Cheers,
Dimitre Novatchev.


__________________________________________________
Do You Yahoo!?
Send your FREE holiday greetings online!
http://greetings.yahoo.com

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]