% -------------------------------
% 1. next_to_last(+,-)

next_to_last([X,_],X).
next_to_last([_|R],X) :- 
   next_to_last(R,X).


% -------------------------------
% 2. wrap_with_f(+,-) 

wrap_with_f([],[]).
wrap_with_f([H|T],[f(H)|R]) :-
   wrap_with_f(T,R).


% -------------------------------
% 3. delete_b(+,-)

delete_b([b|L], L).
delete_b([H|T], [H|R]) :-
	delete_b(T,R).

delete_one_b([b|L], L).
delete_one_b([H|T], [H|R]) :-
	\+( H = b ),
	delete_one_b(T,R).


% alternative for delete_one_b/2:
%
%delete_one_b([b|L], L). :- !.
%delete_one_b([H|T], [H|R]) :-
%	delete_one_b(T,R).


% -------------------------------
% 4. in_list(+,-)

in_list([H|T],[H|R]) :- 
	check_elements(T,R).
in_list(T,[_|L]) :- 
	in_list(T,L).

check_elements([],_).
check_elements([H|T],[H|R]) :-
	check_elements(T,R).

% -------------------------------
% 5. last_added_first(+,-) 

last_added_first([X],[X,X]).
last_added_first([H|T],[X,H|T]):-
   last_added_first(T,[X|T]).

% another solution:
%
%last_added_first([First|R],[Last,First|T]) :-
%   addAux(R,T,Last).
%
%addAux([Last],[Last],Last).
%addAux([H|R],[H|T],Last) :-
%   addAux(R,T,Last).


% another solution:
%
%last_added_first(In,[Last|In]) :-
%  find_last(In,Last).
%
%find_last([H],H).
%find_last([_|T],X) :-
%  find_last(T,X).



% -------------------------------
% 6. same_number(+) 
% accepts strings of the form a^n b^n

same_number(X) :-
	same_number(X,[]).

same_number([b|X],[b|X]).
same_number([a|X],Y) :-
	same_number(X,[b|Y]).

% -------------------------------
% 7. same_number_mixed(+) 

same_number_mixed(X) :-
	same_number_mixed(X,[],[]).

same_number_mixed([],L,L).
same_number_mixed([a|Rest],ExpBs,FoundBs) :-
	same_number_mixed(Rest,[b|ExpBs],FoundBs).
same_number_mixed([b|Rest],ExpBs,FoundBs) :-
	same_number_mixed(Rest,ExpBs,[b|FoundBs]).


% -------------------------------
% 8. mix(+,-)

mix([],[]).
mix([H|T],L) :-
	mix(T,Aux),
	insert(H,Aux,L).

insert(X,L,[X|L]).
insert(X,[H|T],[H|R]) :-
	insert(X,T,R).
