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

by by@REDACTED
Mon Sep 16 15:58:23 CEST 2019


Well, if we can put the type check on entry, we can put the type check before we call this function :-)

Yao

> 在 2019年9月16日,21:53,zxq9 <zxq9@REDACTED> 写道:
> 
> 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.
> 
>> I will say that I've been using languages in the Lisp family for
>> a little over 40 years, and this has been the very *least* of my
>> problems.
> 
> The whole issue boils down to the above. I can see some trivial merit in doing a type check over Ys at the outset (since we'll crash on bad Xs in the actual procedure) but this business of moving to the last element to check whether the list is properly formed is both insane and almost certainly a legacy code killer, as there are a handful of systems out there that depend on being able to manipulate improper lists for whatever reason.
> 
> I didn't follow this thread closely, but I'm surprised I didn't see doing a single type check on entry as a possibility. What issues would make this a bad idea?
> 
> -Craig
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
> 




More information about the erlang-questions mailing list