[erlang-questions] Dict size in guards
Fri Feb 11 10:40:12 CET 2011
FWIW, Doug Clark found that the average length of lists in large, mature
lisp applications was about four.
<http://portal.acm.org/citation.cfm?id=359423.359427>Even if f() [see below]
were statistically tuned, however, the basic problem remains -- you wouldn't
be able to use that f() function in a guard, and you CAN use length() in a
guard, despite a possible compromise of the "soft real-time" promise.
How hard would it be to make length() execute in (short) constant time,
e.g., with an actual length attribute? I know this implies consing up a
length for each list returned by any evaluation, so (except for
length()-heavy list computation) it would tend to hurt performance. But if
the compiler has good live-variable analysis, it might be possible to avoid
computing list lengths (and storing them) in the overwhelming majority of
cases. Some such data flow analysis might also (with a little work) do
double-duty by making more opportunistic garbage collection of temporary
lists possible. For applications where space is more important than time,
the extra space required for storing list lengths could then be *much* more
than made up for, with cdr-coding and other time-honored tricks for reducing
On Fri, Feb 11, 2011 at 5:54 PM, Tony Rogvall <tony@REDACTED> wrote:
> And in general guard you can use something like this:
> f(L) ->
> if is_list(tl(tl(tl(tl(tl(tl(tl(tl(tl(tl(tl(L)))))))))))) ->
> is_list(tl(tl(tl(tl(tl(tl(L))))))) -> medium_case(L);
> true -> short_case(L)
> It compiles a LOT better than the corresponding pattern version.
> On 11 feb 2011, at 09.03, Ulf Wiger wrote:
> > On 11 Feb 2011, at 02:02, Richard O'Keefe wrote:
> >> The inclusion of length/1 in guard tests has always been more than
> >> a bit dodgy. It's better taste to avoid it. Not least because
> >> if you do something like
> >> f(L)
> >> when length(L) > 10 -> long_case(L);
> >> when length(L) > 5 -> medium_case(L);
> >> when true -> short_case(L).
> >> doesn't do what people often think. I've several times been
> >> caught by that.
> > Also, as pointed out by Kostis in his Tidier presentations, a fairly
> > common use is
> > f(L) when length(L) == 0 -> …
> > which of course has to traverse the whole (potentially very long) list.
> > BR,
> > Ulf
> > Ulf Wiger, CTO, Erlang Solutions, Ltd.
> > http://erlang-solutions.com
> > ________________________________________________________________
> > erlang-questions (at) erlang.org mailing list.
> > See http://www.erlang.org/faq.html
> > To unsubscribe; mailto:erlang-questions-unsubscribe@REDACTED
> "Have run Make so many times I dunno what's installed anymore"
More information about the erlang-questions