Subject: Re: [SWIPL] Q

From: "Michael A. Covington" <[email-masked]>
Date: Thu, 16 Aug 2001 09:39:28 -0400

> "Andrew V. Diatchkov" <[email-masked]> wrote:
> Type this:
>
> ?- A = '[]',B=[],A=B.
> A = []
> B = []
> Yes
>
> Why '[]' is [] ?
>
> What did you expect it to be?  It's perhaps more accurate to say
> that [] is '[]':
>     name([], Name), Name = "[]".

There's a very simple reason for expecting [] to be something other than
a 2-character atom.  It denotes an empty list.  Now, you and I know that
[] is an atom even though [ and ] with anything else between them is a
list.  But that's a deliberate (and probably useful) ambiguity in the
language.  It would be more consistent if [] were a list with zero
elements.  In fact however [] is not a list at all; it's an atom that we
use to denote an empty list.

> You will also find that
> X = (a,b),
> functor(X, F, 2),
> X = ','
> comes out true,

This is one of Prolog's three uses of commas, another place where the
syntax trips up beginners.

> and that
> A = '{}', B = {}, A = B.
> comes out true.

Again because {} is an atom with very special properties... {a,b} is
'{}' with  a,b  as its argument, and  a,b  is in turn a joined to b by
the infix operator ','   that is to say it is ','(a,b) just as a+b is
'+'(a,b).  It's a rather sticky piece of syntactic sugar for DCG rules.

All these are very amusing bits of syntactic sugar but hardly obvious to
anyone without great experience with the language.

Ah well... In Fortran and in non-ANSI BASIC, there are hardly any
general syntactic rules, and every different statement is sui generis!
And in Lisp everything has the same syntax (and (it (looks (like
(this))))).