Yalnız Mesajı Göster

Cevap : Prolog Örnekleri | Prolog Examples

Eski 04-07-2009   #11
[KAPLAN]
Varsayılan

Cevap : Prolog Örnekleri | Prolog Examples



Program 102 - CLP(R) and Prolog programs for converting Celsius and Fahrenheit

:- ensure_loaded(library(clpr))


/* ************************************************ */
/* */
/* convert_clpr/2 */
/* Arg 1: temperature in degrees Celsius */
/* Arg 2: temperature in degrees Fahrenheit */
/* Summary: Converts between Celsius and */
/* Fahrenheit */
/* Author: P J Hancox */
/* Date: 27 November 2004 */
/* */
/* ************************************************ */

convert_clpr(Celsius, Fahrenheit) :-
{ Celsius = (Fahrenheit - 32) * 5 / 9 }


/* ************************************************ */
/* */
/* convert_pl/2 */
/* Arg 1: temperature in degrees Celsius */
/* Arg 2: temperature in degrees Fahrenheit */
/* Summary: Converts Fahrenheit into Celsius */
/* Author: P J Hancox */
/* Date: 27 November 2004 */
/* */
/* ************************************************ */

convert_pl(Celsius, Fahrenheit) :-
Celsius is (Fahrenheit - 32) * 5 / 9


/* ************************************************ */
/* */
/* End of program */
/* */
/* ************************************************ */

Program 103 - CLP(FD) program for solving a puzzle

:- ensure_loaded(library(clpfd))


/* ************************************************ */
/* */
/* board/1 */
/* Arg 1: board as a structured object */
/* Summary: Define the domains of the variables */
/* representing the board; imposes */
/* constraints on these variables; */
/* generates the solutions by */
/* "labelling" */
/* Author: P J Hancox */
/* Date: 27 November 2004 */
/* */
/* ************************************************ */

board(square(A1, A2, A3, A4, A5, A6,
B1, B2, B3, B4, B5, B6,
C1, C2, C3, C4, C5, C6,
D1, D2, D3, D4, D5, D6,
E1, E2, E3, E4, E5, E6,
F1, F2, F3, F4, F5, F6)) :-

A1 #= 1,
B3 #= 1,
B6 #= 3,
C3 #= 2,
D6 #= 2,
E1 #= 3,
E2 #= 6,
F3 #= 4,
F5 #= 5,

% define the domain - ordered by rows
domain([A1, A2, A3, A4, A5, A6,
B1, B2, B3, B4, B5, B6,
C1, C2, C3, C4, C5, C6,
D1, D2, D3, D4, D5, D6,
E1, E2, E3, E4, E5, E6,
F1, F2, F3, F4, F5, F6], 1, 6),

% impose constraints on rows
all_different([A1, A2, A3, A4, A5, A6]),
all_different([B1, B2, B3, B4, B5, B6]),
all_different([C1, C2, C3, C4, C5, C6]),
all_different([D1, D2, D3, D4, D5, D6]),
all_different([E1, E2, E3, E4, E5, E6]),
all_different([F1, F2, F3, F4, F5, F6]),

% impose constraints on columns
all_different([A1, B1, C1, D1, E1, F1]),
all_different([A2, B2, C2, D2, E2, F2]),
all_different([A3, B3, C3, D3, E3, F3]),
all_different([A4, B4, C4, D4, E4, F4]),
all_different([A5, B5, C5, D5, E5, F5]),
all_different([A6, B6, C6, D6, E6, F6]),

% impose constraints on groups
% groups could be constrained as with rows and columns,
% but this is an alternative (longer) way of imposing constraints
A1 #\= A2, A1 #\= A3, A1 #\= A4, A1 #\= B2, A1 #\= C2,
A2 #\= A3, A2 #\= A4, A2 #\= B2, A2 #\= C2,
A3 #\= A4, A3 #\= B2, A3 #\= C2,
A4 #\= B2, A4 #\= C2,
B2 #\= C2,

A5 #\= A6, A5 #\= B3, A5 #\= B4, A5 #\= B5, A5 #\= C3,
A6 #\= B3, A6 #\= B4, A6 #\= B5, A6 #\= C3,
B3 #\= B4, B3 #\= B5, B3 #\= C3,
B4 #\= B5, B4 #\= C3,
B5 #\= C3,

B1 #\= C1, B1 #\= D1, B1 #\= D2, B1 #\= D3, B1 #\= E2,
C1 #\= D1, C1 #\= D2, C1 #\= D3, C1 #\= E2,
D1 #\= D2, D1 #\= D3, D1 #\= E2,
D2 #\= D3, D2 #\= E2,
D3 #\= E2,

B6 #\= C4, B6 #\= C5, B6 #\= C6, B6 #\= D6, B6 #\= E6,
C4 #\= C5, C4 #\= C6, C4 #\= D6, C4 #\= E6,
C5 #\= C6, C5 #\= D6, C5 #\= E6,
C6 #\= D6, C6 #\= E6,
D6 #\= E6,

D4 #\= D5, D4 #\= E5, D4 #\= F4, D4 #\= F5, D4 #\= F6,
D5 #\= E5, D5 #\= F4, D5 #\= F5, D5 #\= F6,
E5 #\= F4, E5 #\= F5, E5 #\= F6,
F4 #\= F5, F4 #\= F6,
F5 #\= F6,

E1 #\= E3, E1 #\= E4, E1 #\= F1, E1 #\= F2, E1 #\= F3,
E3 #\= E4, E3 #\= F1, E3 #\= F2, E3 #\= F3,
E4 #\= F1, E4 #\= F2, E4 #\= F3,
F1 #\= F2, F1 #\= F3,
F2 #\= F3,

% generate the solution by labelling
labeling([], [A1, A2, A3, A4, A5, A6]),
labeling([], [B1, B2, B3, B4, B5, B6]),
labeling([], [C1, C2, C3, C4, C5, C6]),
labeling([], [D1, D2, D3, D4, D5, D6]),
labeling([], [E1, E2, E3, E4, E5, E6]),
labeling([], [F1, F2, F3, F4, F5, F6])


/* ************************************************ */
/* */
/* solve/0 */
/* Summary: Constrains and generates solutions to */
/* the puzzle, outputing successful */
/* solutions */
/* Author: P J Hancox */
/* Date: 27 November 2004 */
/* */
/* ************************************************ */

solve :-
board(Square),
display_board(Square)


/* ************************************************ */
/* */
/* display_board/1 */
/* Arg 1: board as a structured object */
/* Summary: Displays the board */
/* Author: P J Hancox */
/* Date: 27 November 2004 */
/* */
/* ************************************************ */

display_board(Board) :-
display_board(0, Board)


/* ************************************************ */
/* */
/* display_board/1 */
/* Arg 1: counter */
/* Arg 2: board as a structured object */
/* Summary: Displays the board */
/* Author: P J Hancox */
/* Date: 27 November 2004 */
/* */
/* ************************************************ */

% 1 - terminating
display_board(36, _Board) :-
nl, nl
% 2 - recursive
display_board(Index, Board) :-
Index < 36,
Index1 is Index + 1,
arg(Index1, Board, Cell),
display_square(Index1, Cell),
display_board(Index1, Board)


/* ************************************************ */
/* */
/* display_square/1 */
/* Arg 1: counter */
/* Arg 2: individual square of the board */
/* Summary: Displays an individual square and, if */
/* it is the 6th square of a row, moves */
/* to a new line */
/* Author: P J Hancox */
/* Date: 27 November 2004 */
/* */
/* ************************************************ */

display_square(Index, Square) :-
( nonvar(Square) ->
write(' '), write(Square), write(' ')
;
write(' ')
),
( (Index) // 6 =:= (Index)/6 ->
nl
;
true
)


/* ************************************************ */
/* */
/* End of program */
/* */
/* ************************************************ */

Kaynak ( Source ): 06-02630 Software Workshop Prolog - Example programs

Alıntı Yaparak Cevapla