[erlang-questions] call by value

Ulf Wiger ulf.wiger@REDACTED
Thu May 19 18:17:29 CEST 2011

On 19 May 2011, at 17:59, Paulo Sérgio Almeida wrote:

> On 5/19/11 4:41 PM, Ulf Wiger wrote:
>> You should ignore the issue of copying. Essentially, Erlang can be viewed as using copy-on-write semantics.
>> The key is whether the expression is evaluated or not (it is) before the function is called, and also whether the called function can modify the input parameters (it can't)*.
>> * Unless you use unholy functions like hipe_bifs:bytearray_update/3, which of course you don't. ;-)
> Unless you need to implement huge bloom filters with good performance. ;-)

Yeah, well, I was bitten by exactly that last week, as I prepared a code kata based on a bloom filter.

I had a bloom filter implementation that used hipe_bifs:bitarray_update/3 for performance and simplicity, but since I was messing with those, I used them also in the code calling on the bloom filter (which was to find all words of a given length).

The fun started when I then made a QLC generator out of the CPS-style word finder. It worked, but instead of returning all words of length N, it returned a list of several instances of the SAME word.

It took me a little while to realise that, as I was using destructive update when permuting the "character vector", the QLC iterator kept mutating the same binary the whole time, and simply returned a pointer to that one binary for each word found.

Mutation is evil…

Ulf W

Ulf Wiger, CTO, Erlang Solutions, Ltd.

More information about the erlang-questions mailing list