They let me carry out the following program in PROLOG, but because I am a little inexperienced in this language, it has been difficult for me to carry out the operation of the largest and smallest population of each country, as well as that of the largest territory, in which it compares a predefined value of a variable but, I think that doing it this way is incorrect, rather it is necessary to order the values of the territory knowledge base and show the name of the largest country and from there the name of the smallest country, but in conditions for separate.
Here I attach my code in advance thank you very much for taking the time to respond.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% PROLOG BASE DE CONOCIMIENTO PAÍSES %
% V 0.1 %
% 01/10/2018 %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Planteamiento del problema ---------------------------------------------------------------
%Se tienen en una base de conocimiento los países de América del Norte y América Central.
% de los cuales se quiere conocer:
% 1.- Su Territorio el Mayor Territorio y el menor territorio de todos los países de la
% base de conocimiento.
% 2.- Con que países tienen fronteras los países de la base de conocimiento.
% 3.- El tamaño de la población entre los cuales dar el menor y mayor país con población.
%Base de conocimiento General.
pais(mexico).
pais(estados_unidos).
pais(canada).
pais(belize).
pais(honduras).
pais(guatemala).
pais(nicaragua).
pais(panama).
pais(costa_rica).
pais(el_salvador).
%Colindancias.
vecino(mexico,estados_unidos).
vecino(mexico,belize).
vecino(mexico,guatemala).
vecino(estados_unidos,mexico).
vecino(estados_unidos,canada).
vecino(canada,estados_unidos).
vecino(guatemala,belize).
vecino(guatemala,mexico).
vecino(guatemala,honduras).
vecino(guatemala,el_salvador).
vecino(belize,guatemala).
vecino(belize,mexico).
vecino(honduras,guatemala).
vecino(honduras,el_salvador).
vecino(honduras,nicaragua).
vecino(el_salvador,honduras).
vecino(el_salvador,guatemala).
vecino(nicaragua,honduras).
vecino(nicaragua,costa_rica).
vecino(costa_rica,nicaragua).
vecino(costa_rica,panama).
vecino(panama,costa_rica).
%funcion de fronteras
colindancias(X,Y):-vecino(X,Y).
%Tamaño del Territorio
territorio(mexico,1964375).
territorio(estados_unidos,9147593).
territorio(canada,9984670).
territorio(belize,22966).
territorio(honduras,112492).
territorio(guatemala,108889).
territorio(nicaragua,130373).
territorio(panama,75420).
territorio(costa_rica,51100).
territorio(el_salvador,21041).
%Tamaño de la poblacion.
pob(mexico,120437599).
pob(estados_unidos,320344593).
pob(canada,37067013).
pob(belize,380010).
pob(honduras,9126789).
pob(guatemala,17382396).
pob(nicaragua,6279344).
pob(panama,4158023).
pob(costa_rica,5003000).
pob(el_salvador,7415233).
%Condiciones sobre el menor o mayor territorio.
%al parecer si agrego el el > no me itera el ciclo y no arroja ningún valor
%al igual con el < no funciona
mayorTerr(X):-M = 32034459,territorio(X,M).
menorTerr(X):-L = 22966,territorio(X,L).
%Condiciones de Poblacion.
mayorpob(X):-M=320344593,pob(X,M).
menorpob(X):-L=380010,pob(X,L).
You must first define a predicate that finds the largest element of a list of tuples, the second element being the one we will use for comparison. The largest element in a list with only one element is that element. The largest element of a list with head
H
and tailT
is the largest element betweenH
and the largest element ofT
:For example:
Now, to find the country with the largest population, you must get a list of all the countries along with their populations, and search for the largest element using the predicate
max/2
:In this case, it
findall/3
finds all the pairs(A,B)
that satisfy the goalpob(A,B)
, and stores them in the listL
. You can define the rest of the predicates analogously.