[erlang-questions] Strings as Lists

David Mercer <>
Fri Feb 15 22:34:01 CET 2008


> When you need to do a manipulation on string in constant length,
> you need reverse. For example, to_upper() can be implemented as

My question was in regards to reversing strings, not lists of characters.
Specifically, Hasan Veldstra's complaint that representing strings as lists
doesn't work when you use lists:reverse to reverse them:

> This would not work on a string with combining characters, e.g. ü  
> represented as u followed by ¨, or a CJKV ideograph.
> 
> A lot of glyphs *cannot* be represented by a single Unicode codepoint. 

Your example is a case on "unreversing" a reversal done during the up-casing
process.

My guess is that in the "ü represented as u followed by ¨" case, it would
work just right: the "u" would be up-cased to "U", and the "¨" would follow
capital "U" (following the list:reverse to unreverse the list).  I don't
think up-casing a CJKV ideograph makes any sense, so you'd probably end up
with the same string you started with.

So the question goes back to Mr. Veldstra (or anyone) as to why you would
want to reverse a Unicode string (unless it is to unreverse a previous
algorithmic reversal, in which case we have no problem with combining
characters).

-----Original Message-----
From: Lev Walkin [mailto:] 
Sent: Friday, February 15, 2008 14:46
To: 
Cc: 'Erlang Questions'
Subject: Re: [erlang-questions] Strings as Lists

David Mercer wrote:
> Out of curiosity, besides as an academic exercise and a point of debate,
> what is the practical use of reversing a string?  I've been programming
for
> a quarter century and am having trouble remembering when I last needed to
> reverse a string.  Search? yes; compare? yes; substitute? yes; modify?
yes.
> Just can't think of when I've needed to reverse a string.  Is there some
> common protocol that requires you to send Unicode strings in reverse order
> or something?


When you need to do a manipulation on string in constant length,
you need reverse. For example, to_upper() can be implemented as

to_upper(L) = foldr(fun(C, A) -> [char_upper(C) | A] end, [], L).

or

to_upper(L) = map(fun char_upper/1, L).

of which both both have O(n) space requirement, or via

	to_upper(L) = lists:reverse(to_upper(L, []))
	to_upper([], Acc) -> Acc;
	to_upper([C|T], Acc) -> to_upper(T, [char_upper(T)|Acc]).

which is O(n) in space.

Both are O(n) in time, though with different coefficients.

-- 
Lev Walkin





More information about the erlang-questions mailing list