home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / lang / prolog / 2273 < prev    next >
Encoding:
Internet Message Format  |  1992-12-21  |  2.2 KB

  1. Path: sparky!uunet!van-bc!cs.ubc.ca!unixg.ubc.ca!kakwa.ucs.ualberta.ca!ersys!lcraft!cduguay
  2. From: cduguay@lcraft.ersys.edmonton.ab.ca (Claude Duguay)
  3. Newsgroups: comp.lang.prolog
  4. Subject: Re: next_to code question
  5. Message-ID: <T243VB2w165w@lcraft.ersys.edmonton.ab.ca>
  6. Date: Sun, 20 Dec 92 07:13:52 MST
  7. References: <1992Dec17.204918.6730@mercury.cair.du.edu>
  8. Organization: LogiCraft Corp, Edmonton, AB, Canada
  9. Lines: 48
  10.  
  11. awinterb@diana.cair.du.edu (Art Winterbauer) writes:
  12. > Here's some code I don't understand.
  13. > append([],L,L) :- !.
  14. > append([X|L1],L2,[X|L3]) :- append(L1,L2,L3). 
  15. > next_to(El1,El2,List) :- append(_,[El1,El2|_], List).
  16. > In the second edition of Clocksin and Mellish, p. 150, they state that the
  17. > above definition of "next_to" is an example of relational programming.  By
  18. > typing:
  19. > next_to(a,b,[c,d,a,b,q]).
  20. > for example, you'll get a true response.  I don't know why it works,
  21. > probably because I never could quite understand how append works.  Since
  22. > I'm missing an important concept here, could someone explain to me (and the
  23. > net) why it works?  The debugger in the Prolog interpreter (Amziod) I have
  24. > doesn't seem to go into enough detail to show me exactly what's happening.
  25. > Thanks,
  26. > Art
  27.  
  28. This is easier to understand if you try to explain it declaratively. Firstly,
  29. the append predicate is nondeterministic and will provide all possible
  30. solutions to List1 + List2 = List3.  The next_to predicate says, in plain
  31. English, that any two elements are in sequence if they are the first two
  32. elements of any back portion for the given list. The append predicate will
  33. return any valid combination and the syntax [El1,El2|_] tests the first two
  34. elements for each list returned (in the second parameter of append).
  35.  
  36. To play around with it, try using append like:
  37.  
  38.    append(Front,Back,[a,b,c,d,e,f])
  39.    
  40. to see what values Back returns. Another way to write next_to would be:
  41.  
  42.    next_to(El1,El2,[El1,El2|_]).
  43.    next_to(El1,El2,[_|Tail]) :- next_to(El1,El2,Tail).
  44.  
  45. This version says, in English, that the elements are next to each other if
  46. they are in sequence as the first two elements of the list OR if they are
  47. next to each other in the Tail of the list.
  48.  
  49. Hope this helps a little...
  50.  
  51. Claude Duguay              cduguay@lcraft.ersys.edmonton.ab.ca 
  52.