2012-04-19 8 views
1

코드에서 쿼리를 어떻게 실행합니까? 예를 들어코드의 프롤로그 쿼리

는 : 사람 (X, Y) :

person(abe,instructor). 
person(bob,student). 
person(cindy,student). 
person(david,student). 
person(abe,student). 

% Like this, but this does not work 
% :- person(X,Y). 

프로그램을로드 한 후에, 나는 다음과 같은 쿼리를 실행할 수 있습니다. 여기

X = abe, 
Y = instructor ; 
X = bob, 
Y = student ; 
X = cindy, 
Y = student ; 
X = david, 
Y = student ; 
X = abe, 
Y = student. 

답변

1

당신은 단지 새로운 조건을 만들 수 있습니다 .. 2 개 가지 방법 : I 프로그램이로드되면, 쿼리 및 출력을 실행할 수 있도록 프로그램 자체의 일환으로이 쿼리를 실행할 수있는 방법

. 첫 번째 사람은 모든 사람 (X, Y)을 찾아 AllPeople 목록에 넣은 다음 기록합니다.

두 번째는 첫 번째 일치를 수행하고 첫 번째 일치를 실행 한 다음 다시 시도하라는 '실패'를 알려주는 '실패 유도 루프'입니다. 일치하지 않을 때까지 계속 진행하고 두 번째 조건자를 찾습니다. 술어가 마침내 true를 리턴하는지 확인하기 위해 동일한 이름.

showpeople1 :- 
    findall(X/Y, person(X,Y), AllPeople), 
    write(AllPeople). 

showpeople2 :- 
    person(X, Y), 
    write(X), write(','), write(Y), nl, 
    fail. 

showpeople2 :- true. 



?- showpeople1. 
[abe/instructor,bob/student,cindy/student,david/student,abe/student] 
true. 

?- showpeople2. 
abe,instructor 
bob,student 
cindy,student 
david,student 
abe,student 
true.