xsl-list
[Top] [All Lists]

[xsl] RE: Cheaper to prepend or append an item to a sequence?

2011-02-22 07:50:21
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_:

[3]

2:[3]

1:[2,3]

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


This appending approach is strongly _discouraged_:

[1]

[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. 

'A':" SMALL CAT"
"A SMALL CAT"

5:[1,2,3,4,5]
[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?

/Roger

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>