mardi 27 novembre 2018

Why doesn't prolog give a correct value for variable?

I am writing a basic program in prolog but I don't get it to work. This is the code:

My prolog code.

borders(sweden,finland,586).
borders(norway,sweden,1619).

allborders(X,Y,L) :- borders(X,Y,L).
allborders(X,Y,L) :- borders(Y,X,L).
addborders(C,Lsum,Set) :- length(Set,0), write(C), write(' - '), write(Lsum), C == Lsum.
addborders(C,Lsum,[H|T]) :- Lsum2 is Lsum + H, addborders(C,Lsum2,T).
helpsetofpredicate(Country,L) :- allborders(Country,_,L).
circumference(C,Country) :- setof(L,helpsetofpredicate(Country,L),Set), addborders(C,0,Set).

(Obs: the borders are just a small sample for a gigantic file but are enough to describe the problem)

So what this program is supposed to do is to sum all the borders to a country and check if the given circumference (C) is the total of a countries circumference (Country). If I were to type

circumference(2205,sweden).

the program gives true, which is expected. But if I type

circumference(C,sweden).

the program gives false. I have put some writes within the code to see which values C and Lsum has and the output is _G962 - 2205. Why doesn't prolog assign a correct value to C instead of giving it a random value?




Aucun commentaire:

Enregistrer un commentaire