The best thing I could come up with so far is this function:
numberFromList([X], X) :- digit(X), !. numberFromList(List, N) :- member(X, List), delete(List, X, LX), numberFromList(LX, NX), N is NX * 10 + X.
where digit/1
is a function verifying if an atom is a decimal digit.
The numberFromList(List, N)
finds all the numbers that can be formed with all digits from List
.
E.g. [2, 3] -> 23, 32
.
but I want to get this result: [2, 3] -> 2, 3, 23, 32
I spent a lot of hours thinking about this and I suspect you might use something like append(L, _, List)
at some point to get lists of lesser length.
I would appreciate any contribution.
You are missing case when you skip digit from list.
numberFromList([X], X) :-
digit(X), !.
numberFromList(List, N) :-
member(X, List),
delete(List, X, LX),
numberFromList(LX, NX),
( % use X
N is NX * 10 + X
; % skip X
N = NX
).
BTW, as @Roland Illig mentioned there is select(X, List, LX)
to replace member(X, List), delete(List, X, LX)