Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Problem 3 */
- % Helper predicates
- contains(X, [X|_]).
- contains(X, [_|T]) :- contains(X, T).
- subset([], _).
- subset([H|T], L) :- contains(H, L), subset(T, L).
- toNumRev([], 0).
- toNumRev([H|T], S) :- toNumRev(T, S2), S is S2*10 + H.
- app([], L2, L2).
- app(.(H,T), L2, .(H,L)) :- app(T, L2, L).
- rev([],[]).
- rev(.(H,T), L) :- rev(T, L0), app(L0, [H], L).
- toNum(L, S) :- rev(L, L2), toNumRev(L2, S).
- wordsum(W1, W2, W3) :- toNum(W1, S1), toNum(W2, S2), toNum(W3, S3), S3 is S1 + S2.
- verbalarithmetic(Letters, Word1, Word2, Word3) :-
- fd_all_different(Letters), /* letters must be unique */
- subset(Letters, [0,1,2,3,4,5,6,7,8,9]), /* letters must be digits */
- \+(nth(1, Word1, 0)), \+(nth(1, Word2, 0)), \+(nth(1, Word3, 0)), /* first letter of each word can't be 0 */
- wordsum(Word1, Word2, Word3).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement