[erlang-questions] strip end off of string

Robert Virding rvirding@REDACTED
Thu May 6 20:26:31 CEST 2010


On 6 May 2010 15:20, Wes James <comptekki@REDACTED> wrote:
> On Wed, May 5, 2010 at 8:08 PM, Richard O'Keefe <ok@REDACTED> wrote:
>>
>
> <snip>
>
> Thanks for your comments on the above :)
>
>> t2(String) ->
>>   t2a(lists:reverse(String)).
>>
>
> The thought just came to me.  Does reverse actually reverse all the
> data in string and work on it or does it just work from the end going
> backward.  It seems there would be a performance hit on long "String"s
> if reverse actually reversed all the items in to another string then
> worked on it.  Do you know how reverse actually does this?

You can code reverse in Erlang as:

reverse(List) -> reverse(List, []).

reverse([H|T], Acc) -> reverse(T, [H|Acc]);
reverse([], Acc) -> Acc.

Now lists:reverse/2 is coded in C but it works in the same way as this
with the same result.

Robert


More information about the erlang-questions mailing list