2012-10-19 3 views
1

내 데이터베이스는 다음과 같은 조건으로 구성됩니다.데이터베이스에서 계산하기

도로 (1,2, geel). road (2,3, blauw). 도로 (1,3, geel).

처음 두 숫자는 점입니다. 모든 지점에 짝수 개의 도로가 있는지 확인해야합니다. 나는 다음 코드로 처리 할 수 ​​있었지만 어떻게 든이 작업을 수행하는 더 좋은 방법이 있다고 생각한다.

% Count(Element, List, Occurences) => Counts the amount of occurences of Element in the given List 
count(_, [], 0). 
count(X, [X | T], N) :- 
    !, count(X, T, N1), 
    N is N1 + 1. 
count(X, [_ | T], N) :- 
    count(X, T, N). 

checkRoad :- 
    findall(Point,(weg(Point,_,_) ; weg(_,Point,_)),List), 
    list_to_set(List,K), 
    foreach((member(P,K), count(P, List,N)), N mod 2 =:= 0). 

답변

0

나는이 방법이 더 나은 성능을 가질 것이라고 생각 :

checkRoad:- 
    findall(Point,(road(Point,_,_) ; road(_,Point,_)),List), % renamed wge/3 with road/3 
    msort(List, SList), 
    checkEven(none, SList). 

checkEven(_, []). 
checkEven(Item, [Item|SList]):- 
    !, 
    checkOdd(Item, SList). 
checkEven(_, [Item|SList]):- 
    checkOdd(Item, SList). 

checkOdd(Item, [Item|SList]):- 
    checkEven(Item, SList). 

당신이 첫 번째 정렬 모든 점은 당신은 모든 지점은 짝수로 표시할지 여부를 테스트하기 위해 한 번이 정렬 된 목록을 통과 할 수있는 경우 도로.

+0

그건 의미가있어, 고마워 :) – user757926