[erlang-questions] Tuples referencing each other problem

Richard A. O'Keefe ok@REDACTED
Mon Sep 18 02:34:43 CEST 2017



On 17/09/17 7:27 PM, Grzegorz Junka wrote:
> Is it possible to create two tuples that reference each other in Erlang?
> Something like:
>
> T1 = {T2}
> T2 = {T1}

No.
(1) When you create a new term in Erlang it must be fully
     specified.
(2) You cannot change it once it has been created.

> My understanding is that when using pointers (i.e. in C++) one needs to
> mutate the first tuple in order to store the reference to the second
> tuple once it has been created.

In Lisp also.  Oddly enough, not in Prolog:
| ?- T1 = {T2}, T2 = {T1}.
T1 = {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{

Erlang is closer to Prolog than it is to C++, but logical variables
are one of the things it did not copy.
>
> This is of course a generalization of a more specific problem of
> creating liked structures like a double linked list or a tree with leafs
> containing links to each other. Are there any techniques that would
> allow creating algorithms operating on such structures in Erlang?

No, and thank goodness!  You wouldn't *believe* how much trouble
such structures are in Lisp.  Or the grief it caused in Prolog.
Just as an example, what do you expect term comparison to do?

Doubly linked lists are almost always a very bad idea.
The back links in the Document Object Model (trees) are
a *stunningly* bad idea, because they prevent sharing.  (When
processing SGML and XML I use a "Document *Value* Model"
library I developed years ago.  It is so much easier and so
much more efficient that it really is not funny.)




More information about the erlang-questions mailing list