%pagina de los Ejercicios: https://prof.ti.bfh.ch/hew1/informatik3/prolog/p-99/
add(X,[],[X]).
add(X,[H|T],[H|L]):-add(X,T,L).
add2(X,L,[X|L]).
miembro(X,[X|_]).
miembro(X,[_|T]):-miembro(X,T).
%1
last_element([X],X).
last_element([_|T],X):-last_element(T,X).
%2
penultimo([X,_],X).
penultimo([_|T],X):-penultimo(T,X).
%3
kElement([H|_],1,H).
kElement([_|T],N,X):- Z is N-1,
kElement(T,Z,X).
%4
lenList([],0).
lenList([_|T],X):-lenList(T,Z),
X is Z+1.
%5
reverse([],[]).
reverse([H1|T1],L):-reverse(T1,T2),
add(H1,T2,L).
%6
palindromo(L):- reverse(L,L1),
L1 == L.
%7
flat([],[]).
flat([H|T],K):-flat(T,L),
is_list(H),
append(H,L,K),!.
flat([H|T],K):-flat(T,L),
add2(H,L,K).
%Parte del 15
addN(X,1,[X]).
addN(X,N,[X|T]):- Z is N-1,
addN(X,Z,T).
%14
duplicar([X],[X,X]).
duplicar([H|T],[H,H|Z]):-duplicar(T,Z).
%15
nplicar([],_,[]).
nplicar([H|T],N,L):-nplicar(T,N,Z),
addN(H,N,X),
append(X,Z,L).
%16
dropKelm([],_,[],_).
dropKelm([_|T],N,L,Z):- N is 1,
dropKelm(T,Z,L,Z).
dropKelm([H|T],N,[H|L],D):- Z is N-1,
dropKelm(T,Z,L,D).
dropKelm(L,N,R):-dropKelm(L,N,R,N).
%17
split([],0,[],[]).
split([H|T],N,L,[H|D]):- N==0,
split(T,0,L,D).
split([H|T],N,[H|D],L):- Z is N-1,
split(T,Z,D,L).
%18
slice([],0,0,[]).
slice([_|T],_,K,L):-K == 0,
slice(T,0,0,L).
slice([H|T],N,K,[H|L]):-N == 0,
Z is K-1,
slice(T,0,Z,L).
slice([_|T],N,K,L):-Z is N-1,
D is K-1,
slice(T,Z,D,L).
%22
range(0,0,[]).
range(N,K,[N|L]):- K == N,
range(0,0,L).
range(N,K,[N|L]):- Z is N+1,
range(Z,K,L).
%20
remove_at(_,[],_,[]).
remove_at(H,[H|T],N,L):-N == 1,
remove_at(H,T,1,L).
remove_at(X,[H|T],N,[H|L]):-Z is N-1,
remove_at(X,T,Z,L).
%21
addK(_,[],_,[]).
addK(X,[H|T],N,[X,H|L]):-N==1,
addK(X,T,-1,L).
addK(X,[H|T],N,[H|L]):-Z is N-1,
addK(X,T,Z,L).
%8
compress([],[]).
compress([H|T],[H|L]):-compress(T,L),
not(miembro(H,L)).
compress([_|T],L):-compress(T,L).
contarE(_,[],0).
contarE(X,[X|T],N):-contarE(X,T,Z),
N is Z+1.
contarE(X,[_|T],N):-contarE(X,T,N).
%9
packList([],[]).
packList([H|T],X):-contarE(H,[H|T],N),
addN(H,N,R),
lenList([H|T],Len),
slice([H|T],N,Len,D),
packList(D,F),
add2(R,F,X).
%10
encode([],[]).
encode([H|T],X):-contarE(H,[H|T],N),
lenList([H|T],Len),
slice([H|T],N,Len,D),
encode(D,F),
add2([N,H],F,X).
%11
encode2([],[]).
encode2([H|T],X):-contarE(H,[H|T],N),
N>1,
lenList([H|T],Len),
slice([H|T],N,Len,D),
encode2(D,F),
add2([N,H],F,X).
encode2([H|T],X):-contarE(H,[H|T],N),
lenList([H|T],Len),
slice([H|T],N,Len,D),
encode2(D,F),
add2(H,F,X).
give([H,X],[H,X]).
%12
decode([],[]).
decode([H|T],X):-is_list(H),
give(H,[N,E]),
addN(E,N,R),
decode(T,D),
append(R,D,X).
decode([H|T],X):-decode(T,D),
add(H,D,X).
rotate(L,N,X):-lenList(L,Len),
slice(L,N,Len,R),
slice(L,0,N,D),
append(R,D,X).
randomList(_,0,[]).
randomList(L,N,[X|T]):-lenList(L,Z),
random(1,Z,D),
remove_at(X,L,D,R),
Y is N-1,
randomList(R,Y,T).
rangeRList(0,_,[]).
rangeRList(N,Lim,[D|T]):-random(1,Lim,D),
Y is N-1,
rangeRList(Y,Lim,T).
randPermut([],[]).
randPermut(L,[X|T]):-lenList(L,N),
N==1,
remove_at(X,L,1,R),
randPermut(R,T).
randPermut(L,[X|T]):-lenList(L,N),
random(1,N,D),
remove_at(X,L,D,R),
randPermut(R,T).
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.