Práctica de Prolog: Parciales (Paradigmas)

De Cuba-Wiki
Saltar a: navegación, buscar
Back.png Volver a la página de la materia

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Funciones de Numeros - Start  %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%es_natural(-Numero).
es_natural(0).
es_natural(N) :- es_natural(N1), N is N1 + 1.

%es_positivo(-Numero).
es_positivo(1).
es_positivo(N) :- es_natural(N1), N is N1 + 1.

%es_negativo(-Numero).
es_negativo(-1).
es_negativo(N) :- es_negativo(N1), N is N1 - 1.

%diferencia_con_siguiente_entero(-Signo, +Numero).
diferencia_con_siguiente_entero(1, 0).
diferencia_con_siguiente_entero(1, N) :- N > 0.
diferencia_con_siguiente_entero(0, N) :- N < 0.

%es_entero(-Signo, +Numero).
es_entero(0).
es_entero(N) :- es_entero(M), NM is -M, diferencia_con_siguiente_entero(S, NM),
N is NM + S.

%signo_aux(-Signo, +Numero).
signo_aux(0, 0).
signo_aux(1, N) :- N > 0.
signo_aux(-1, N) :- N < 0.

%signo(-Signo, -Numero).
signo(0, 0).
signo(S, N) :- es_entero(N), N \= 0, signo_aux(S, N).

%en_rango(-Numero, +Desde, +Hasta).
en_rango(D, D, H) :- D =< H.
en_rango(N, D, H) :- D =< H, ND is D + 1, en_rango(N, ND, H).

%desde(-Numero, +Desde).
desde(D, D).
desde(N, D) :- DD is D+1, desde(N, DD).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Funciones de Numeros - End  %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Funciones de Arboles - Start  %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%es_arbol_de_altura(-Arbol, +Altura).
es_arbol_de_altura(nil, 0).
es_arbol_de_altura(bin(I, _, D), H) :- H > 0, H1 is H - 1,
en_rango(HI, 0, H1),
en_rango(HD, 0, H1), H1 is max(HD, HI),
es_arbol_de_altura(I, HI), es_arbol_de_altura(D, HD).

%altura(-Arbol, -Altura).
altura(A, H) :- es_natural(H), es_arbol_de_altura(A, H).

%preorder_de_arboles(-Nodos, -Arbol).
preorder_de_arboles([], []).
preorder_de_arboles(L, [nil | AS]) :- preorder_de_arboles(L, AS).
preorder_de_arboles([X | XS], [bin(I, X, D) | AS]) :- preorder_de_arboles(XS, [I, D | AS]).

%preorder(-Nodos, -Arbol).
preorder(L, A) :- preorder_de_arboles(L, [A]).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Funciones de Arboles - End  %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%