[erlang-questions] behavior of lists:append/1

Raimo Niskanen raimo+erlang-questions@REDACTED
Wed Sep 18 14:00:50 CEST 2019


On Tue, Sep 17, 2019 at 11:25:11PM +0900, zxq9 wrote:
> On 2019/09/17 22:13, Raimo Niskanen wrote:
> > On Mon, Sep 16, 2019 at 10:53:57PM +0900, zxq9 wrote:
> >> On 2019/09/16 11:49, Richard O'Keefe wrote:
> >>> The fact that L is a well-formed list is verified N times,
> >>> for a total cost O(N * |L|).  But with the current definition,
> >>> the cost is O(N), independent of |L|.
> >>
> >> Hm... just to beat a dead horse...I suppose we could get away with a
> >> single check:
> >>
> >>     -export([append/2]).
> >>
> >>     append(Xs, Ys) when is_list(Ys) -> combine(Xs,Ys).
> >>
> >>     combine([X | Xs], Ys) -> [X | combine(Xs, Ys)];
> >>     combine([],       Ys) -> Ys.
> > 
> > No.
> > 
> > lists:append/2 can be used and *is* used by existing code to construct
> > improper lists.  It would break backwards compatibility.

...to construct improper lists even as in appending a tail element (that is
need not be a list) to a list...

> 
> The accept/2 -> combine/2 combination accepts improper lists just fine. 
> That was the whole point and I addressed the detail of actually 
> *needing* to accept improper lists for the sake of legacy code. It only 
> checks whether the 'Ys' is a list *at all* before proceeding.
> 
> The beaten horse is now absolutely craven for some abuse.
> 
> -Craig
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions

-- 

/ Raimo Niskanen, Erlang/OTP, Ericsson AB



More information about the erlang-questions mailing list