% PD 2007-08: practica_3b_sol.pl (10 de Diciembre de 2007)
% Departamento de Ciencias de la Computación e I.A.
% Universidad de Sevilla 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Ejercicio 1 Definir, usando append, la  relación pertenece(X,L) que se
% verifique si X es un elemento de la lista L. Por ejemplo, 
%    ?- pertenece(b,[a,b]).
%    Yes
%    ?- pertenece(c,[a,b]).
%    No
%    ?- pertenece(X,[a,b]).
%    X = a ;
%    X = b ;
%    No
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Ejercicio 2 Definir la relación sublista(L1,L2) que se verifique si L1
% es una sublista de L2. Por ejemplo, 
%   ?- sublista([b,c],[a,b,c,d]).
%   Yes
%   ?- sublista([b,d],[a,b,c,d]).
%   No
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Ejercicio 3 Definir la relación último(X,L) que se verifique si X es
% el último elemento de la lista L. Por ejemplo,  
%     ?- último(X,[a,b,c]).
%     X = c ;
%     No
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Ejercicio 4 Definir la relación inversa(L1,L2) que se verifique si L2
% es la lista L1 en orden inverso, Por ejemplo,  
%   ?- inversa([a,b,c],L).
%   L = [c, b, a] ;
%   No
%   ?- inversa([a,[b,c],d,e],L).
%   L = [e, d, [b, c], a] ;
%   No
%  Nota: La relación predefinida correspondiente a inversa es reverse.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Ejercicio 5 Definir la relación palíndromo(L) que se verifique si L es
% un palíndromo; es decir, da igual leerlo de izquierda a derecha que
% leerlo de derecha a izquierda. Por ejemplo, 
%    ?- palíndromo([r,o,m,a,y,a,m,o,r]).
%    Yes
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Ejercicio 6 Definir la relación selecciona(X,L1,L2) que se verifique
% si X es un elemento de la lista L1 y L2 es la lista de los restantes
% elementos. Por ejemplo, 
%   ?- selecciona(X,[a,b,c],L).
%   X = a     L = [b, c] ;
%   X = b     L = [a, c] ;
%   X = c     L = [a, b] ;
%   No
% Nota: La relación predefinida correspondiente a selecciona es select. 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Ejercicio 7 Definir la relación máximo(X,Y,Z) que se verifique si Z es
% el máximo de los números X e Y. Por ejemplo, 
%    ?- máximo(3,5,Z).
%    Z=5
%    ?- máximo(2,3,X). 
%    X = 3
%    ?- máximo(3,2,X). 
%    X = 3
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Ejercicio 8 (Algoritmo de Euclides) Dados dos enteros positivos X e Y,
% el máximo común divisor (mcd) D puede obtenerse de la siguiente
% manera: 
%  * Si X e Y son iguales, entonces D es igual a X
%  * Si X<Y, entonces D es igual al máximo común divisor de X y la
%    diferencia Y-X. 
%  * Si Y<X entonces hacemos lo mismo que en caso anterior con X e Y
%    intercambiados. 
% Definir la relación mcd(X,Y,D) que calcule el mcd D de los enteros
% positivos X e Y. 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Ejercicio 9 Definir la relación longitud(L,N) que se verifique si N es
% la longitud de la lista L. Por ejemplo, 
%    ?- longitud([],N).
%    N = 0 
%    ?- longitud([a,b,c],N).
%    N = 3 
%    ?- longitud([a,[b,c]],N).
%    N = 2
% Nota: La relación predefinida correspondiente a longitud es length.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Ejercicio 10 Definir la relación máximo_lista(L,N) que se verifique si
% N es el máximo de los elementos de la lista de números L. Por ejemplo,
%    ?- máximo_lista([1,3,9,5],N). 
%    N = 9
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Ejercicio 11 Definir la relación entre(N1,N2,X) que se verifique si X
% es mayor o igual que N1 y menor o igual que N2. Por ejemplo,
%    ?- entre(2,5,X). 
%    X = 2 ; 
%    X = 3 ; 
%    X = 4 ;  
%    X = 5 ; 
%    No 
%    ?- entre(2,1,X). 
%    No
% Nota: La relación predefinida correspondiente a entre es between.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Ejercicio 12 (Base de datos familiar)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% En este ejercicio vamos a considerar el programa familia.pl del Tema
% 8. Las personas se representan mediante estructuras de la
% forma 
persona([Nombre,Apellido_1,Apellido_2],
        fecha(Día,Mes,Año),
        trabajo(Profesión,Sueldo))
% La base de datos de las familias se definen por la relación
% familia(P,M,L) que se verifica si existe una familia con padre P,
% madre M y lista de hijos L. 
familia(persona([tomas,garcia,perez],
                fecha(7,mayo,1950),
                trabajo(profesor,75)),
        persona([ana,lopez,ruiz],
                fecha(10,marzo,1952),
                trabajo(medica,100)),
        [ persona([juan,garcia,lopez],
                  fecha(5,enero,1970),
                  estudiante),
          persona([maria,garcia,lopez],
                  fecha(12,abril,1972),
                  estudiante) ]).
familia(persona([jose,perez,ruiz],
                fecha(6,marzo,1953),
                trabajo(pintor,150)),
        persona([luisa,galvez,perez],
                fecha(12,mayo,1954),
                trabajo(medica,100)),
        [ persona([juan_luis,perez,perez],
                  fecha(5,febrero,1980),
                  estudiante),
          persona([maria_jose,perez,perez],
                  fecha(12,junio,1982),
                  estudiante),
          persona([jose_maria,perez,perez],
                  fecha(12,julio,1984),
                  estudiante) ]).
% La relación casado(X) que se verifica si X es un hombre casado y la
% relación casada(X) que se verifica si X es un mujer casada.  
casado(X) :-
   familia(X,_,_).

casada(X) :-
   familia(_,X,_).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Ejercicio 12.1 Definir la relación hijo(X) que se verifique si X figura
% en alguna lista de hijos. 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Ejercicio 12.2 Definir la relación existe(X) que se verifique si X es
% una persona existente en la base de datos. 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Ejercicio 12.3 Hacer una pregunta tal que que las respuestas sean las
% listas de la forma [nombre, apellido1, apellido2] de todas las
% personas que existen.  
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Ejercicio 12.4 Determinar todos los estudiantes nacidos antes de 1983.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Ejercicio 12.5 Definir la relación fecha_de_nacimiento(X,Y) de forma
% que si X es una persona, entonces Y es su fecha de nacimiento.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Ejercicio 12.6 Buscar todos los hijos nacidos en 1982.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Ejercicio 12.7 Definir la relación sueldo(X,Y) que se verifique si el
% sueldo de la persona X es Y. 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Ejercicio 12.8 Buscar todas las personas nacidas antes de 1954 cuyo
% sueldo sea superior a 72 euros.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Ejercicio 12.9 Definir la relación total(L,Y) que se verifique si Y es
% la suma de los sueldos de las personas de la lista L. 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Ejercicio 12.10 Calcular los ingresos totales de cada familia.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Ejercicio 13 (Simulación del autómata)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% En este ejercicio consideraremos el programa automata.pl del Tema
% 8. El autómata se define mediante las siguientes relaciones 
%  * final(E) que se verifica si E es el estado final.
%  * trans(E1,X,E2) que se verifica si se puede pasar del estado E1 al
%    estado E2 usando la letra X. 
%  * nulo(E1,E2) que se verifica si se puede pasar del estado E1 al
%    estado E2 mediante un movimiento nulo. 
% El autómata del ejemplo está definido por
final(e3).

trans(e1,a,e1).
trans(e1,a,e2).
trans(e1,b,e1).
trans(e2,b,e3).
trans(e3,b,e4).

nulo(e2,e4).
nulo(e3,e1).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Ejercicio 13.1 Definir la relación acepta_acotada(S,L,N) que se
% verifique si el autómata, a partir del estado S, acepta la lista L y
% la longitud de L es N.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Ejercicio 13.2  Buscar las cadenas aceptadas a partir de e1 con
% longitud 3. 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Ejercicio 13.3 Definir la relación acepta_acotada_2(S,L,N) que se
% verifique si el autómata, a partir del estado S, acepta la lista L y
% la longitud de L es menor o igual que N.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Ejercicio 14 (Banda de músicos)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Se sabe que
%  1. Una banda está compuesta por tres músicos de distintos paises y
%     que tocan distintos instrumentos. 
%  2. El pianista toca primero.
%  3. Juan toca el saxo y toca antes que el australiano.
%  4. Marcos es francés y toca antes que el violinista.
%  5. Hay un músico japonés.
%  6. Un músico se llama Saúl.
%  Determinar el nombre, el país y el instrumento que toca cada uno de
%  los músicos de la banda.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Ejercicio 15 (Movimientos del caballo)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Supongamos que los cuadros del tablero de ajedrez los representamos
% por pares de números [X,Y] con X e Y entre 1 y 8.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Ejercicio 15.1 Definir la relación salta(+C1,?C2) que se verifique si
% el caballo puede pasar en un movimiento del cuadrado C1 al cuadrado
% C2. Por ejemplo,  
%     ?- salta([1,1],S).
%     S=[3,2];
%     S=[2,3];
%     No
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Ejercicio 15.2 Definir la relación camino(L) que se verifique si L es
% una lista de cuadrados representando el camino recorrido por un
% caballo sobre un tablero vacío. Por ejemplo, 
%     ?- camino([[1,1],C]).
%     C=[3,2];
%     C=[2,3];
%     No
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Ejercicio 15.3 Usando la relación camino, escribir una pregunta para
% determinar los caminos de longitud 4 por los que puede desplazarse un
% caballo desde cuadro [2,1] hasta el otro extremo del tablero (Y=8) de
% forma que en el segundo movimiento pase por el cuadro [5,4]. 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Ejercicio 15.4 Calcular el menor número de movimientos necesarios para
% desplazar el caballo del cuadro [1,1] al [2,2]. ¿Cuántos caminos de
% dicha longitud hay de [1,1] a [2,2]? 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

