deep list

Difference between version 4 and 5 - Previous - Next
A '''deep [list]''' is a [data format] for representing an [tree%|%orderedtree]:  Each item in the list can be identified as either a simpngle value or a
sequence of values.  Because a simple value might look like a standard Tcl
list, some care is needed.



** See Also **

   [list] and [Depth of a List]:   [escargo] ruminates about differentiating between a single value and a sequence of values in a list.

   [ycl%|%ycl list deep], by [PYK]:   An implementation of a deep list.  Provides `index`, `insert`, `is struct`, `range`, and `set`.



** Description **

Built-in Tcl `[list]` routines provide no grammar to distinguish between a listitem that is a simpngle value and an list item that is nested structure.  The
 caller 
of `[lindex]` or `[lset]` dictates the desired interpetation by passing
 index 
arguments.  Sometimes, however, the caller would like to discover the
 structure 
of the data rather than dictating it.  IfA slightly richer data format imakes rich
enough, thie
s structure could be discoverable.
For an ordered tree tThe standard `[list]` notation already provides information about
 the order.  Inf
Tormation about whether an dditem is a siongle valuely opr sequence of
values is also needed.  This additioInal information can be encodedut as
additionalhe structure. of Athe tree, a simpngle 
value is encoded as a list containing only one
 item.  AIf a value that doeisn't look like a list 
containing more thanly one item, then that value is
 itself interpreted as a deep 
list.  The following exvamplue is a list
 containing two items:

======
one
{
    {{two three}}
    {two three}
}
======
The first item is the simpngle value `one`.  The The second item is a list in
which the first item is the value `two three` encoded as a list containing one
item, and the second item is a deep
 list containing the values `two` and 
`three`, each of which is already formatted as a list containing one value due
to the nature of the representtion of a list.

Looking again at that second item in isolation:

======
{{two three}}
{two three}
======
The first item is a list containingd only one item, sowhich its value ines thed value of
that one istem, e.g. `two three`.  It is easily distinguishable from the second
first item, which idecodes anto ther dsingleep valisue `two three`. 

When adding values to a list, use `[list]` to tagencode each item that that is 
meant as a
 single value rather than a sequence of values:.

======
lappend tree [list $item]
======
means "th$istem is not a singlested strvalucture", whereas 

======
lappend tree $item
======
means "th$istem is a nested structure if it lookis a likst containing more than one
value."

Implementations such as `[ycl%|%ycl list deep]` that understand this structuresupply routines that automatically unlistdecode simpngle values whenas thney are
rdetrieved.



<<categories>> list | tree