Thread View: comp.lang.prolog
3 messages
3 total messages
Started by Art Winterbauer
Thu, 17 Dec 1992 20:49
next_to code question
Author: Art Winterbauer
Date: Thu, 17 Dec 1992 20:49
Date: Thu, 17 Dec 1992 20:49
26 lines
1343 bytes
1343 bytes
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 -- Art Winterbauer N0OQS Internet: awinterb@du.edu OR awinterb@diana.cair.du.edu Packet: n0oqs @ w0gvt.#neco.co.usa
Re: next_to code question
Author: Jens Kilian
Date: Sat, 19 Dec 1992 00:23
Date: Sat, 19 Dec 1992 00:23
21 lines
1165 bytes
1165 bytes
> append([],L,L) :- !.
^
This cut is the culprit.
It makes append/3 determinate, so it won't be able to deliver multiple
solutions.
Just replace the first clause by
append([], L, L).
Jens.
--
Internet: jensk@hpbeo82.bbn.hp.com HPDESK : JENS_KILIAN%XU@HP1200
MausNet: Jens Kilian @ BB KILIAN_JENS/HP1200_XU@hpbbi4
Phone: (0|+49)7031-14-4308 TELNET : 778-4308
Fax : (0|+49)7031-14-2049
-------------------------------------------------------------------------------
As the air to a bird, or the sea to a fish, so is contempt to the contemptible.
Re: next_to code question
Author: Claude Duguay
Date: Sun, 20 Dec 1992 14:13
Date: Sun, 20 Dec 1992 14:13
48 lines
2361 bytes
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
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads