我想问一下prolog编程。我有:
byCar(auckland,hamilton).
byCar(valmont,metz).
byTrain(metz,frankfurt).
byPlane(frankfurt,bangkok).
byPlane(bangkok,auckland).
travell(From,To,go(From,To,car)) :- byCar(From,To).
travell(From,To,go(From,To,train)) :- byTrain(From,To).
travell(From,To,go(From,To,plane)) :- byPlane(From,To).
travell(From,To,go(From,Step,Via,Go)) :- travell(From,Step,go(From,Step,Via)),travell(Step,To,Go).
然后我在winprolog中问:
?- travell(valmont,hamilton,Go).
回答
Go = go(valmont,metz,car,go(metz,frankfurt,train,go(frankfurt,bangkok,plane,go(bangkok,auckland,plane,go(auckland,hamilton,car)))))
我的问题是,是否可以计算它有多少'去'?就像我的例子之前它有5'去'
答案 0 :(得分:-1)
会更容易。无论如何,试试
count_last_arg(Struct,Func,Count) :-
count_last_arg(Struct,Func,0,Count).
count_last_arg(Struct,Func,N,Count) :-
Struct =.. L, last(L, LL), LL =.. [Func|_]
-> N1 is N+1,
count_last_arg(LL,Func,N1,Count)
; Struct =.. [Func|_]
-> Count is N+1
; Count is N.