[erlang-questions] Question about lists:filter

David Mercer dmercer@REDACTED
Fri Aug 30 15:17:38 CEST 2013


On Thursday, August 29, 2013, Richard A. O'Keefe wrote:

> On 30/08/2013, at 1:52 AM, David Mercer wrote:
> 
> >
> > Curiously enough, though, timingwise the following 2 seem to do
> roughly the same:
> >
> > is_palindrome1(X) ->
> > 	integer_to_list(X) == lists:reverse(integer_to_list(X)).
> >
> > is_palindrome2(X) ->
> > 	Digits = integer_to_list(X),
> > 	Digits == lists:reverse(Digits).
> >
> 
> Presumably integer_to_list/1 does the bulk of its work in C.
> There are two kinds of cost involved in code like this: the time it
> takes to build the garbage and the space the garbage takes.  The two
> versions both create 3N list cells which almost instantly become
> garbage.

Why does the second one create 3N cells.  Isn't it only 2: Digits and the result of lists:reverse(Digits)?

DBM




More information about the erlang-questions mailing list