length() in guards

Fredrik Linder <>
Tue Sep 30 13:35:37 CEST 2003


The observation made by O'Keefe is imho a good one.

is_length(L, 3)         % same as is_length(L, '==', 3)
is_length(L, '=<', 3)   % does not check value of tl(tl(tl(L)))

(Did I get then number of calls to tl/1 right? (:-)

/Fredrik

> -----Original Message-----
> From: 
> [mailto:]On Behalf Of Richard A.
> O'Keefe
> Sent: den 30 september 2003 05:53
> To: 
> Subject: Re: length() in guards
> 
> 
> Raimo Niskanen <> wrote:
> 	We have already thought of a possible optimization in the
> 	compiler to convert such as "length(L) > 3" to an internal
> 	"length_gt(L, 3)", but it was not possible without changing the
> 	semantics of length(L).
> 
> Oh *curse*, that's right.  Worse still, on examining the latest sources,
> I see that there _are_ cases when length/1 is used to test 
> whether something
> is a proper list or not.  What a nuisance.
> 
> The observation remains:  
>  - *most* of the uses of length/1 in guards could use a bounded-time
>    test because they are known to be given lists;
>  - *most* of the rest could be rewritten with an improvement in both
>    speed and clarity.
> 
> A set of length_{lt,eq,gt,ge,ne,le}/2 guard tests, however spelled,
> would be useful.
> 
> 



More information about the erlang-questions mailing list