EN → DE A-AA+ Sitemap Search

# 8    Prolog Potpourri (or Fun with Prolog)

### Solution of Exercise 8

Prolog Uebungen 8: Prolog Potpouri

## 8.1    Crypto-arithmetic Puzzle

``````/* call:  ?- puzzle( [0,S,E,N,D], [0,M,O,R,E], [M,O,N,E,Y]), M=1 */
/* solution: puzzle( [0,9,5,6,7], [0,1,0,8,5], [1,0,6,5,2])      */

puzzle( X, Y, Z) :-
puzzle( X, Y, Z, 0, 0, [0,1,2,3,4,5,6,7,8,9], _).
puzzle( [ X1| Xs], [ Y1| Ys], [ Z1| Zs], C_in, C_out, Digs_in, Digs_out) :-
puzzle( Xs, Ys, Zs, C_in, C_out1, Digs_in, Digs_out1),
choose( X1, Digs_out1, DigsX),
choose( Y1, DigsX, DigsY),
choose( Z1,DigsY, Digs_out),
add1( X1, Y1, Z1, C_out1, C_out).

puzzle( [], [], [], 0, 0, Digs, Digs).

choose( X, Y, Y) :- nonvar( X), !.
choose( X, Y, Z) :-
delete( X, Y, Z).

delete( X, [ X| Xs], Xs).
delete( X, [ X1| Xs], [ X1| Zs]) :- delete( X, Xs, Zs).

add1( X, Y, Z, Cin, Cout) :-
S is X + Y + Cin,
Z is S mod 10, Cout is S div 10.
``````

Back to example 8.1

## 8.2    Three Friends

``````/*        p( Name,    Job,           City,    Rank) */
/* Solution: daniel,  sport_teacher, basel,   1
michael, doctor,        zurich,  2
bill,    officer,       bern,    3
*/

friends( S)  :-
S = [p( _, _, _, 1), p( _, _, _, 2), p( _, _, _, 3)],
member( p( bill, _, _, _), S),
member( p( _, _, zurich, _), S),
member( p( michael, doctor, _, RM), S),
member( p( _, _, bern, RB), S), RM < RB,
member( p( daniel, _, basel, RD), S),
member( p( _, officer, _, RBe), S), RD < RBe,
member( p( _, sport_teacher, _, 1), S).
``````

Back to example 8.2

## 8.3    An Intelligence Competion

``````iq( Seq, X) :- hypothesis( R, Seq), test_apply( R, Seq, X).

hypothesis( lin_diff( A, B), [X1, X2, X3| _]) :-
A is X3 - 2*X2 + X1,
B is X2 - X1 - A.

hypothesis( fibo, [1, 1, 2| _]).

test_apply( lin_diff( A, B), Seq, X) :-
test_apply(lin_diff( A, B), Seq, 1, X).

test_apply( fibo, [ X1, X2, X3| Xs], X) :-
X3 is X1 + X2,
test_apply( fibo, [X2, X3| Xs], X).
test_apply( fibo, [ XN, XN1], X) :-
X is XN + XN1.

test_apply( lin_diff( A, B), [XN, XN1| Xs], N, X) :-
XN1 is XN + A * N + B, N1 is N + 1,
test_apply( lin_diff( A, B), [ XN1| Xs], N1, X).
test_apply( lin_diff( A, B), [ XN], N, X) :-
X is XN + A * N + B.

/* Testing
?- iq( [ 2, 4, 6], X)           => X = 8
?- iq( [ 4, 7, 12, 19, 28], X)  => X = 39
?- iq( [ 1, 1, 2, 3], X)        => X = 5
?- iq( [ 1, 2, 4, 7], X)        => X = 11
*/

``````

Back to example 8.3

## 8.4    Language Translation

``````/* SWI Prolog - dynamic predicates: */
:- dynamic sequence/2.
:- dynamic word/2.
:- dynamic dictionary/1.

init :-
D = 'c:/tmp/code_08_04_dict.pro',
retractall(dictionary(_)),
assert(dictionary(D)),
consult( D).

translate( [ G1| Gs], EE) :-
sequence( [ G1| G], E),
append( [ G1| G], Grest, [ G1| Gs]),
translate( Grest, Erest),
append( E, Erest, EE).

translate( [ G1| Gs], [ E1| Es]) :-
word( G1, E1), translate( Gs, Es).

translate( [ G1| Gs], [ ?| Es]) :-
\+ word( G1, _),
translate(Gs, Es).

translate( [], []).

learn( G, E) :-
is_list( G), !, assert( sequence( G, E)), ok.

learn( G, E) :- assert( word( G, E)), ok.

ok :- write( 'OK, teacher!'), nl.

save :-
dictionary(File),
tell( File),
write(':- dynamic sequence/2.'), nl,
write(':- dynamic word/2.'), nl,
save_words, save_sequences,
told, !.

save_words :-
word( D, E),
write( word( D, E)),
write( .), nl,
fail.

save_words.

save_sequences :-
sequence( D, E),
write( sequence( D, E)),
write( .), nl,
fail.

save_sequences.

is_list( []).
is_list( [ _| _]).

member( X, [ X| _]).
member( X, [ _| Y]) :- member( X, Y).

append( [], Y, Y).
append( [ X1| Xs], Ys, [ X1| Zs]) :-
append( Xs, Ys, Zs).

/* Testing Session
?- init.
?- translate( [ er, geht, nach, hause], E)   => E = [ he, goes, home]
?- translate( [ er, ist, zu, hause], E)      => E = [ he, ?, at, home]
?- learn( ist, is).    => OK, teacher!
?- translate( [ das, haus, ist, gross], E)   => E = [ ?, ?, is, ?]
?- translate( [ wie, geht, es, ihnen], E)    => E = [ ?, goes, ?, ?]
?- learn( [ wie, geht, es, ihnen], [ how, are, you])
=> OK, teacher!
?- translate( [ wie, geht, es, ihnen], E)    => E = [ how, are, you]
?- save.
*/

``````

Back to example 8.4