check_flips1([],L,L).

check_flips1([Flip|MoreFlips],Cakes,Result) :-
    length(OnSpatula, Flip),
    append(OnSpatula,Rest,Cakes),
    reverse(OnSpatula,Flipped),
    append(Flipped,Rest,Result0),
    check_flips1(MoreFlips,Result0,Result).

check_flips(L) :- findall(Flips, fsort(L,Flips), Results), member(Flips, Results), \+nextto(X,X,Flips), check_flips1(Flips,L,Result),
    format('Stack: ~w\nFlips: ~w\nResult: ~w; ', [L, Flips, Result]),
    msort(Result,Result),
	writeln('OK'),!.

check_flips(L) :- fsort(L,Flips), nextto(X,X,Flips), writeln('Consecutive identical flips'),!.
check_flips(_) :- writeln('Didn\'t sort').

					
:-[fsort].
