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

RE: Cheaper to prepend or append an item to a sequence

Subject: RE: Cheaper to prepend or append an item to a sequence?
From: "Costello, Roger L." <costello@xxxxxxxxx>
Date: Tue, 22 Feb 2011 08:50:07 -0500
 RE: Cheaper to prepend or append an item to a sequence
Hi Folks,

Thank you for your responses. They are surprising.

In the functional language Haskell, there is a clear disadvantage of appending
an item onto the end of a list, and developers are strongly encouraged to
build lists by prepending an item onto a list. For example, this prepending
approach is strongly _preferred_:




This results in creating the list: [1,2,3]

This appending approach is strongly _discouraged_:


[1] ++ [2]

[1,2] ++ [3]

This results in creating the same list: [1,2,3]

Here is what one Haskell book says:

Watch out when repeatedly using the ++ operator on long strings. When you put
together two lists (even if you append a singleton list to a list, for
instance: [1,2,3] ++ [4]), internally, Haskell has to walk through the whole
list on the left side of ++. That's not a problem when dealing with lists that
aren't too big. But putting something at the end of a list that's fifty
million entries long is going to take a while. However, putting something at
the beginning of a list using the : operator (also called the cons operator)
is instantaneous.


> 5:[1,2,3,4,5]

Notice how : takes a number and a list of numbers or a character and a list of
characters, whereas ++ takes two lists. Even if you're adding an element to
the end of a list with ++, you have to surround it with square brackets so it
becomes a list.

I vaguely recall that Lisp behaving the same way (as Haskell) and with the
same recommendation to build lists from the left not the right.

Why is XSLT different from these other functional languages?


Current Thread


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.
First Name
Last Name
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-2013 All Rights Reserved.