cptbon(L,X):-
cptf(L,L2),
dans(L2,X),
cptfa(L,L2).
dans([X|_],X).
dans([_|L],X):-
dans(L,X).
cptfa(L,L).
cptfa(L1,L2):-
cpt(L1,L3),
cptfa(L3,L2),
write(L3),nl.
cptf(L,L).
cptf(L1,L2):-
cpt(L1,L3),
cptf(L3,L2).
cpt([X|Q1],[X|Q2]):-
cpt(Q1,Q2).
cpt([X|Q1],Q2):-
cpt2(X,Q1,Q2).
cpt2(X,[A|Q1],[A|Q2]):-
cpt2(X,Q1,Q2).
cpt2(X,[Y|Q1],[Z|Q1]):-
Z is X+Y;
X>Y, Z is X-Y;
X<Y, Z is Y-X;
Z is X*Y;
X>Y,0 is X mod Y, Z is X/Y;
X<Y,0 is Y mod X, Z is Y/X.
[^] # En Prolog
Posté par DIAL . En réponse à la dépêche Kitsune 2.0 : jeu de lettres et de chiffres. Évalué à 3.
mettre par ex
cptbon([2,3,25,7,1,3],347).
*/
cptbon(L,X):-
cptf(L,L2),
dans(L2,X),
cptfa(L,L2).
dans([X|_],X).
dans([_|L],X):-
dans(L,X).
cptfa(L,L).
cptfa(L1,L2):-
cpt(L1,L3),
cptfa(L3,L2),
write(L3),nl.
cptf(L,L).
cptf(L1,L2):-
cpt(L1,L3),
cptf(L3,L2).
cpt([X|Q1],[X|Q2]):-
cpt(Q1,Q2).
cpt([X|Q1],Q2):-
cpt2(X,Q1,Q2).
cpt2(X,[A|Q1],[A|Q2]):-
cpt2(X,Q1,Q2).
cpt2(X,[Y|Q1],[Z|Q1]):-
Z is X+Y;
X>Y, Z is X-Y;
X<Y, Z is Y-X;
Z is X*Y;
X>Y,0 is X mod Y, Z is X/Y;
X<Y,0 is Y mod X, Z is Y/X.