🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.prolog
Article #2232

Re: next_to code question

#2232
From: Claude Duguay
Date: Sun, 20 Dec 1992 14:13
48 lines
2361 bytes
awinterb@diana.cair.du.edu (Art Winterbauer) writes:
> Here's some code I don't understand.
> 
> append([],L,L) :- !.
> append([X|L1],L2,[X|L3]) :- append(L1,L2,L3). 
> 
> next_to(El1,El2,List) :- append(_,[El1,El2|_], List).
> 
> In the second edition of Clocksin and Mellish, p. 150, they state that the
> above definition of "next_to" is an example of relational programming.  By
> typing:
> 
> next_to(a,b,[c,d,a,b,q]).
> 
> for example, you'll get a true response.  I don't know why it works,
> probably because I never could quite understand how append works.  Since
> I'm missing an important concept here, could someone explain to me (and the
> net) why it works?  The debugger in the Prolog interpreter (Amziod) I have
> doesn't seem to go into enough detail to show me exactly what's happening.
> 
> Thanks,
> 
> Art

This is easier to understand if you try to explain it declaratively. Firstly,
the append predicate is nondeterministic and will provide all possible
solutions to List1 + List2 = List3.  The next_to predicate says, in plain
English, that any two elements are in sequence if they are the first two
elements of any back portion for the given list. The append predicate will
return any valid combination and the syntax [El1,El2|_] tests the first two
elements for each list returned (in the second parameter of append).

To play around with it, try using append like:

   append(Front,Back,[a,b,c,d,e,f])
   
to see what values Back returns. Another way to write next_to would be:

   next_to(El1,El2,[El1,El2|_]).
   next_to(El1,El2,[_|Tail]) :- next_to(El1,El2,Tail).

This version says, in English, that the elements are next to each other if
they are in sequence as the first two elements of the list OR if they are
next to each other in the Tail of the list.

Hope this helps a little...

Claude Duguay              cduguay@lcraft.ersys.edmonton.ab.ca 
Technical Details
Message-ID: <T243VB2w165w@lcraft.ersys.edmonton.ab.ca>
Path: arc1-ca.pugleaf.net!sparky!uunet!van-bc!cs.ubc.ca!unixg.ubc.ca!kakwa.ucs.ualberta.ca!ersys!lcraft!cduguay
References: <1992Dec17.204918.6730@mercury.cair.du.edu>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!van-bc!cs.ubc.ca!unixg.ubc.ca!kakwa.ucs.ualberta.ca!ersys!lcraft!cduguay
From: cduguay@lcraft.ersys.edmonton.ab.ca (Claude Duguay)
Newsgroups: comp.lang.prolog
Subject: Re: next_to code question
Message-ID: <T243VB2w165w@lcraft.ersys.edmonton.ab.ca>
Date: Sun, 20 Dec 92 07:13:52 MST
References: <1992Dec17.204918.6730@mercury.cair.du.edu>
Organization: LogiCraft Corp, Edmonton, AB, Canada
Lines: 48