Re: Ann: SWI-Prolog 7.1.0
Jan Wielemaker <[email protected]>
| Newsgroups | gmane.comp.ai.prolog.swi |
|---|---|
| Message-ID | <[email protected]> |
On 11/22/2013 08:30 PM, Aleksey wrote:
>> Using '[]' as list terminator prevents dynamic distinction between atoms and lists.
> As a result, we cannot use type polymorphism that involve both atoms and lists.
> For example, we cannot use /multi lists/ (arbitrary deeply nested lists) of atoms.
> Multi lists of atoms are a in some situations a good representation of a flat list that
> is assembled from sub sequences. The alternative, using difference lists or DCGs is
> often less natural and sometimes demands for `opening' proper lists (i.e., copying
> the list while replacing the terminating empty list with a variable) that have to be
> added to the sequence.
>
> Could you please give some simple example.
There are several typical cases. One concerns that you map data from a
language that can uniquely identify lists from symbols, such as JSON.
Such languages cannot be mapped naturally to Prolog because it is
unclear whether [] is some kind of identifier or an empty list. Another
are expressions such as consult([]), which may mean to consult the file
'[].pl' or consult nothing. Same for format("", []) with double quotes
set to 'codes': does this write '' (nothing) or '[]'?
In practical programming, you have cases where you have a list of
`tokens' (atoms) and you want to explode some tokens or other special
symbols into a sequence. The clean way is to use DCGs or difference list
to create a nice and flat holding the expansion. In some cases however,
it is way simpler if you can just explode the objects into lists, so you
get [ 'The', [quick, brown, fox], jumps, over, the, [lazy dog] ].
This is all fine, until '[]' may appear as a token in the list. In that
case, flatten/2 just removes it. Due to the ambiguity of the empty list,
creating a multi list and call flatten/2 is considered bad programming.
Without this ambiguity, using multi lists is safe and can be a handy
solution, in particular for the not-so-experienced Prolog programmer.
Cheers --- Jan