[erlang-questions] when any why to use improper lists
Fri Jun 29 02:35:17 CEST 2018
A tuple of 2 elements will take 3 words of memory minimum
tuple1 = [ headerWord, element1, element2 ]
A cons cell has no header word, so an improper list of 1 element and 2nd
element as a tail, just 2 values stored side to side
(same as normal list below except that only 1 cons cell is used, 2 words)
cons1 = [ element1, element2 ]
A proper list of 2 elements will take 2 cons cells, i.e. 4 words of memory
cons2 = [ element2,  ]
cons1 = [ element1, cons2 ]
2018-06-29 2:23 GMT+02:00 Dmitry Belyaev <be.dmitry@REDACTED>:
> It's a way to reduce memory footprint.
> Tuple of size N is roughly represented in memory as an array [TupleTag, N,
> TupleElement1, TupleElement2, ..., TupleElementN].
> Compare it to Cons cell representation: [ConsTag, HeadElement,
> TailElement] - it saves 1 word per structure.
> Kind regards,
> Dmitry Belyaev
> On Fri, Jun 29, 2018 at 9:50 AM, Karlo Kuna <kuna.prime@REDACTED> wrote:
>> dealing with digraph module i have noticed use of improper lists as
>> representations of edges:
>> ['$e' | 123]
>> is there a good reason to use improper lists instead of tuple for this
>> and in general
>> when is a good idea to use improper lists?? (i can't think of example for
>> justified use)
>> erlang-questions mailing list
> erlang-questions mailing list
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the erlang-questions