使用序言(地铁站)计算直接连接到给定车站的车站数量

Count number of stations directly connected to a given station using prolog(metro stations)

这是事实

%first line
connected(new_elmarg,elmarg).
connected(elmarg,ezbet_elnakhl).
connected(ezbet_elnakhl,ain_shams).
connected(ain_shams,elmatareyya).
connected(elmatareyya,helmeyet_elzaitoun).
connected(helmeyet_elzaitoun,hadayeq_elzaitoun).
connected(hadayeq_elzaitoun,saray_elqobba).
connected(saray_elqobba,hammamat_elqobba).
connected(hammamat_elqobba,kobri_elqobba).
connected(kobri_elqobba,manshiet_elsadr).
connected(manshiet_elsadr,eldemerdash).
connected(eldemerdash,ghamra).
connected(ghamra,alshohadaa).
connected(alshohadaa,urabi).
connected(urabi,nasser).
connected(nasser,sadat).
connected(sadat,saad_zaghloul).
connected(saad_zaghloul, alsayyeda_zeinab).
connected(alsayyeda_zeinab,elmalek_elsaleh).
connected(elmalek_elsaleh,margirgis).
connected(margirgis,elzahraa).
connected(elzahraa,dar_elsalam).
connected(dar_elsalam,hadayeq_elmaadi).
connected(hadayeq_elmaadi,maadi).
connected(maadi,thakanat_elmaadi).
connected(thakanat_elmaadi,tora_elbalad).
connected(tora_elbalad,kozzika).
connected(kozzika,tora_elasmant).
connected(tora_elasmant,elmaasara).
connected(elmaasara,hadayeq_helwan).
connected(hadayeq_helwan,wadi_hof).
connected(wadi_hof,helwan_university).
connected(helwan_university,ain_helwan).
connected(ain_helwan,helwan).
%second line
connected(shobra_elkheima,koliet_elzeraa).
connected(koliet_elzeraa,mezallat).
connected(mezallat,khalafawy).
connected(khalafawy,sainte_teresa).
connected(sainte_teresa,road_elfarag).
connected(road_elfarag,massara).
connected(massara,alshohadaa).
connected(alshohadaa,ataba).
connected(ataba,naguib).
connected(naguib,sadat).
connected(sadat,opera).
connected(opera,dokki).
connected(dokki,bohooth).
connected(bohooth,cairo_university).
connected(cairo_university,faisal).
connected(faisal,giza).
connected(giza,omm_elmisryeen).
connected(omm_elmisryeen,sakiat_mekki).
connected(sakiat_mekki,elmounib).

我必须在不使用内置谓词 (findall ...) 的情况下计算直接连接到给定站的站数。

Input: are_connected(sadat,X).
Output: X = 4.
Input: are_connected(helwan,X).
Output: X = 1.

我的代码:

are_connected(A,0) :-
        connected(_,A).
are_connected(A,0) :-
        connected(A,_).
are_connected(A,S) :-
    are_connected(A,News),!,
    S is News + 1.

注意:打印的“0”个数应该是输出,但我不知道如何!

注意:connected(,)中漏掉的原子是下划线(_)。

如果不允许使用findall/3,那么可以使用动态谓词来存储计数:

:- dynamic(myflag/2).

myflag(Name, Old, New) :-
   ( retract(myflag(Name, Old)),
     !
   ; true ),
   Val is New,
   assertz(myflag(Name, Val)).

示例:

?- myflag(counter,_,0), myflag(counter,A,A+1), myflag(counter,B,B+1), myflag(counter,C,C).
A = 0,
B = 1,
C = 2.

然后,使用谓词 myflag/3,您可以定义以下谓词:

are_connected(A, N) :-
   myflag(counter, _, 0),          % initialize counter
   (   (   connected(_, A)
       ;   connected(A, _) 
       ),
       myflag(counter, V, V+1),    % increment counter
       fail                        % backtracks to find another solution
   ;   myflag(counter, N, N)       % get final counter value
   ).

示例:

?- are_connected(sadat, X).
X = 4.

?- are_connected(helwan, X).
X = 1.

在 SWI-Prolog 中,您可以使用 内置谓词 flag/3,而不是 myflag/3

are_connected(A, N) :-
   flag(counter, _, 0),          % initialize counter
   (   (   connected(_, A)
       ;   connected(A, _) 
       ),
       flag(counter, V, V+1),    % increment counter
       fail                      % backtracks to find another solution
   ;   flag(counter, N, N)       % get final counter value
   ).