Subject: Re: [SWIPL] Q

From: "Lyosha" <[email-masked]>
Date: Thu, 16 Aug 2001 18:24:45 -0700

If [] is not a list then I would expect is_list([]) to fail. However it
succeeds, which
of course we can treat as an exception. :)

I have another question about lists though.

?- A is [2].
A = 2

?- A is [4]*[5].
A = 20

I expected to get an exception when using lists in 'is'.
Like I would get if I type:
?- A is [1.2].
ERROR: '.'/2: Type error: `integer' expected, found `1.2'

It's not to be complained about I guess but I am curious - why is it
happening?
Anyone know?

> > "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))))).
>
>
>
> ----------------
> * To UNSUBSCRIBE, please use the HTML form at
>
>     http://www.swi.psy.uva.nl/projects/SWI-Prolog/index.html#mailinglist
>
> or send mail to [email-masked] using the Subject:
"unsubscribe"
> (without the quotes) and *no* message body.
>
> ** An ARCHIVE of this list is maintained at
>
>     http://www.swi.psy.uva.nl/projects/SWI-Prolog/mailinglist/archive/