Subject: Re: Prolog examples

From: "Richard A. O'Keefe" <[email-masked]>
Date: Wed, 29 Mar 2000 14:30:32 +1200 (NZST)

Ada Winters <[email-masked]> 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.