[erlang-questions] List to proplist?

Richard Carlsson <>
Wed Oct 29 13:58:11 CET 2008


Hans Bolinder wrote:
> [Richard O'Keefe:]
>> A version of unfold that I prefer is
>>
>> unfold(State, Splitter) ->
>>      unfold(State, Splitter, []).
>>
>> unfold(State, Splitter, Acc) ->
>>      case Splitter(State)
>>        of []            -> lists:reverse(Acc)
>>         ; [Item|State1] -> unfold(State1, Splitter, [Item|Acc])
>>      end.
> 
> Thanks!
> 
> lists:unfold/2 has been added to the upcoming R12B-5 release.

I was going to comment on this before, but didn't get around to it.
Please don't use the return convention above; it is not normal
Erlang programming style, and introduces an improper list for no
really good reason. A more traditional return convention would
use e.g., {Item,State1} | 'none'.

This kind of decision might have far-reaching effects on how other
code that uses an "iterator" approach is coded in the future. I think
it would be a good thing if there was some kind of inofficial standard
convention for iterator-style programming in Erlang. For example, there
is some iterator functionality in gb_trees, that uses the convention
{NextKey,NextValue,NextState} | 'none'. In retrospect, it should have
been {{NextKey,NextValue},NextState}, to fit the {Item,State1} pattern.
But I think it is a bad idea to use cons cells with arbitrary terms as
"tail". (For one thing, Dialyzer will hate it.)

    /Richard



More information about the erlang-questions mailing list