# How to write Prolog prgrame ?

39 views

### chandana nalin

Jan 23, 2017, 11:41:02 AM1/23/17
to SWI-Prolog
Five cars [Red,Blue,Black,White,Gray]
at least one car in front of red and at least one car behind the blue
at least one car front of the black and at least one behind it
white car is ahead the blue
gray and blue are not adjacent to the black

### Barb Knox

Jan 23, 2017, 1:45:16 PM1/23/17
to SWI-Prolog
(1)  What are you wanting to solve for?  (I can easily guess in this case, but you might be surprised at the number of requests for homework help that neglect to actually say what kind of answer the program is to produce.)

(2)  What have you done so far?  Most people here will not just do your homework for you.  Show us where you got stuck.

(3)  Hint: use backtracking.  If you do not yet know what backtracking is then please go away and actually study your course material.

--
---------------------------
|  BBB                b    \    Barbara at LivingHistory stop co stop uk
|  B  B   aa     rrr  b     |
|  BBB   a  a   r     bbb   |   ,008015L080180,022036,029037
|  B  B  a  a   r     b  b  |   ,047045,L014114L4.
|  BBB    aa a  r     bbb   |
-----------------------------

### Barb Knox

Jan 25, 2017, 2:53:53 PM1/25/17
to SWI-Prolog
On 24 Jan 2017, at 07:45, Barb Knox <Bar...@LivingHistory.co.uk> wrote:

On 24 Jan 2017, at 05:41, chandana nalin <chanda...@gmail.com> wrote:

Five cars [Red,Blue,Black,White,Gray]
at least one car in front of red and at least one car behind the blue
at least one car front of the black and at least one behind it
white car is ahead the blue
gray and blue are not adjacent to the black

(1)  What are you wanting to solve for?  (I can easily guess in this case, but you might be surprised at the number of requests for homework help that neglect to actually say what kind of answer the program is to produce.)

(2)  What have you done so far?  Most people here will not just do your homework for you.  Show us where you got stuck.

(3)  Hint: use backtracking.  If you do not yet know what backtracking is then please go away and actually study your course material.

Your lack of reply indicates that you probably haven't done anything so far on this assignment.

Here's an outline to get you started.  Replace the "???"s with the appropriate Prolog goals:

%! cdrs(-Red:between(1,5), -Blue:between(1,5), -Black:between(1,5),
%!      -White:between(1,5), -Gray:between(1,5) ) is nondet.
%
%  Generates the Car orDeRS that satisfy the 4 conditions:
%  1:  at least one car in front of red and at least one car behind the blue.
%  2:  at least one car front of the black and at least one behind it.
%  3:  white car is ahead the blue.
%  4:  gray and blue are not adjacent to the black .
%
%  @author Barbara Knox
%  @version 2017-01-26
%
cdrs(Red, Blue, Black, White, Gray) :-
permutation([1,2,3,4,5], [Red,Blue,Black,White,Gray]),  % Backtracks to here.
/* 1: */  1 < Red, Blue < 5,
/* 2: */  ??? ,
/* 3: */  ??? ,
/* 4: */  ??? .