Prolog: find all numbers of unique digits that can be formed from a list of digits

Posted by animo on Stack Overflow See other posts from Stack Overflow or by animo
Published on 2010-05-26T22:08:43Z Indexed on 2010/05/26 22:11 UTC
Read the original article Hit count: 169

Filed under:
|
|
|

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.

© Stack Overflow or respective owner

Related posts about list

Related posts about numbers