Search code examples
prologclpfd

labeling results into List?


I want write a predicate that can sum_all the lucky number using the predict lucky/5

:- use_module(library(clpfd)).

lucky(A,B,C,D,N) :-
L = [A,B,C,D],
L ins 0..9,
N #= A*1000+B*100+C*10+D,
A+B #= C+D,
labeling([],L).

sum_all():-
% I want to write a code that computes the sum of all lucky numbers

Solution

  • if your Prolog has library(aggregate), you can do

    ?- aggregate_all((count,sum(N)), lucky(_,_,_,_,N), (Count,Sum)).
    Count = 670,
    Sum = 3349665.