lundi 2 mars 2015

Swapping the prefix and suffix in Prolog

I have the following code:



%Intial call to exists_acc-swapPrefixSuffix
swapPrefixSuffix(L, SL, R):- swapPrefixSuffix(SL, L, R, []).

%BASE CASE:
swapPrefixSuffix(SL, L, R, Prefix):- write(SL), append(SL, Suffix, L), write('here'), append(Suffix, SL, Temp),
append(Temp, Prefix, R).

%RECURSIVE CASE: PUTS THE PREFIX IN THE ACC
swapPrefixSuffix([Head| SL], [Y|L], R, Prefix):- Head \= Y, nl, write('R1'), nl,
append(Prefix, [Y], NewAcc), write(NewAcc),
swapPrefixSuffix([Head|SL], L, R, NewAcc).


The problem is that it doesn't work for



swapPrefixSuffix([a,b,c,d,e], SL, [b,c,d,e,a]).


It doesn't give the case for when SL = [].


Aucun commentaire:

Enregistrer un commentaire