From ok@atlas.otago.ac.nz  Wed Mar 29 04:30:36 2000
Received: from atlas.otago.ac.nz (atlas.otago.ac.nz [139.80.32.250])
	by swi.psy.uva.nl (8.9.3/8.9.3) with ESMTP id EAA00779
	for <prolog@swi.psy.uva.nl>; Wed, 29 Mar 2000 04:30:34 +0200 (MET DST)
Received: (from ok@localhost)
	by atlas.otago.ac.nz (8.9.3/8.9.3) id OAA16102;
	Wed, 29 Mar 2000 14:30:32 +1200 (NZST)
Date: Wed, 29 Mar 2000 14:30:32 +1200 (NZST)
From: "Richard A. O'Keefe" <ok@atlas.otago.ac.nz>
Message-Id: <200003290230.OAA16102@atlas.otago.ac.nz>
To: diving@nehp.net, prolog@swi.psy.uva.nl
Subject: Re:  Prolog examples

Ada Winters <diving@nehp.net> asked for help with some homework,
or so it would appear.

	1.  Write a predicate dislay_list with arity 1 to display an
	entire list w/o any user prompts where each list element appears
	on a spearate line with its sequence number and it works for the
	empty list.
	
This is the least helpful solution I can think of:

    display_list(Items) :-
        append(Skipped, [Item|_], Items),
        length(Skipped, Number_Of_Items_Skipped),
        Index is 1 + Number_Of_Items_Skipped,
        write(Index), write(': '), write(Item), nl,
        fail ; true.

It is unclear what "WORKS for the empty list" is supposed to mean;
this version quietly does nothing.

	2.  Write a predicate tohead with arity 3 that will move an
	element to the head of a list:

	tohead(c, [a, b, c, d], X).
	X = [c, a, b, d]
	tohead(c, [x, y, z], X).
	no
	
DRAW A PICTURE:

          +------+-+-----+          +-+------+-----+
          |Before|X|After|	=>  |X|Before|After|
          +------+-+-----+	    +-+------+-----+

How do you take lists apart and put them together again?  append/3

to_head(Item, List, Permuted) :-
    append(Before, [Item|After], List),
    append([Item|Before], After, Permuted).

	3.  Write a Prolog predicate has_no_intersection of arity 2 that
	succeeds if the set intersection of two given list parameters is
	empty.
	
Draw a picture of when the predicate should fail, and then deny it.
	A = [...,X,...]
	B = [...,X,...]

do_not_intersect(A, B) :-    
    \+ (append(_, [X|_], A), append(_, [X|_], B)).


	4.  Write a Prolog predicate that will find the route from one
	location to another.

	intersect( smith, duncan ).
	intersect( nashville, asheville ).
	intersect( asheville, moses ).
	route ( smith, moses, X )
	X = [ smith, asheville, moses ]
	route( smith, smith, X ).
	X = [ smith ]
	route( moses, smith, X )
	no

Please use a less sickening layout style.
Putting spaces after opening brackets or before closing
brackets isn't _completely_ unforgiveable, but you really
don't want to know about the penance required to get forgiveness.

This one you can find in Clocksin & Mellish.

