ITEC 380 - Prolog List Matching and User Input
%This session demonstrates direct database entry.  Comments are marked by "%" 

cantor> prolog
XSB Version 1.4.1 (94/11/21)
[sequential, single word, optimal mode]
| ?- p(X).
Undefined predicate/function: p/1  % Note that p is not in the database

no
| ?- [user].                       % Allow direct entries into database
[Compiling user]
p([a,b,c]).                        % Put this fact into the database
^D                                 % Control D marks the end of direct user input
[user compiled, cpu time used: 0.0099998 seconds]
[user loaded]
yes
| ?- p(X).                         
X = [a,b,c]                        % Now you can see that p([X,Y,Z]) is in the DB
yes

| ?- p([X]).
no

| ?- p([X,Y]).
no

| ?- p([X,Y,Z]).
X = a
Y = b
Z = c 
yes

| ?- p([X|Y]).
X = a
Y = [b,c] 
yes

| ?- p([X, Y|Z]).
X = a
Y = b
Z = [c]
yes

| ?- consult(user).                % You can use consult rather than [] to compile
[Compiling user]
q([a, [b, [c]]]).                  % Add q([...]). to DB
[user compiled, cpu time used: 0.011 seconds] % Ended input w/ ^D
[user loaded]
yes

| ?- q(X).
X = [a,[b,[c]]]
yes

| ?- q([X]).
no

| ?- q([X,Y]).
X = a
Y = [b,[c]]
yes

| ?- q([X,Y,Z]).
no

| ?- q([X|Y]).
X = a
Y = [[b,[c]]]
yes

| ?- q([X, Y|Z]).
X = a
Y = [b,[c]]
Z = []
yes




Last modified on