[erlang-questions] per function/process locals

ok <>
Wed May 2 03:36:44 CEST 2007


On 2 May 2007, at 8:44 am, James Hague wrote,
about tail recursive loops with many arguments simulating
simple imperative variables:

> I actually do this quite a bit.

Why?  Is there an example you can provide?

> Even just having 5 values
> getting passed around and "updated" in a loop bugs me.

Updating 5 variables in a C loop would bug me, most of the time.

> I think the Erlangyest solution is to implement Joe's structs proposed
> a few years ago, then have the compiler recognize situations where
> structs are created and modified in a loop.

If the compiler recognised and special-cased similar code involving
tuples, then the existing records would do the job just as well.
There are reasons to expect this optimisation to be easier than the
other.

 >> I'd rather have the let-for-then construct.

> Could you give an example?

A quick search for 'let then for in fold' in the Erlang mailing list
archive found this:

... we had a thread about looping not so very long ago.
By the end of it, there was a concrete proposal for a construct looking
like

     (let p1 = i1 then s1, ..., pk = ik then sk
      for generators-and-filters
      in final-result
     )

[Added in May 2007: "p" suggests "pattern", "i" suggests "initial  
value",
"s" suggests "step", generators-and-filters is whatever can currently
follow "||" in a list comprehension, final-result is an expression.]

As a trivial example, let's compute the mean of the positive elements
of a list:

     (let N = 0 then N+1, S = 0 then S+X
      for X <- List, X > 0
      in S/N
     )

It's pretty much what you get if you hybridise Scheme DO loops with
list comprehension, and it's as pure as anyone could wish for.

[Added in May 2007: best of all, it doesn't add a new kind of "variable"
to the language.  Nor does it require heavy-duty optimisation to recover
the intended structure.  The only tricky thing is the rebinding of the
pattern variables.]




More information about the erlang-questions mailing list