So my problem is this i have this predicate which is n_aleatorios(El,INF,SUP,L) in which El is the lenght of the list, INF is the lower limit value of the list, SUP is the upper limit value of the list, and L is a list.
The objective of this predicate is to create a list of random integers.
The problem is that this is giving true as a result instead of unifying and giving the list as the result.
auxiliar predicates:
/*Checks if a number isnt a member of a list*/
nao_membro(_, []).
nao_membro(X, [P | R]) :- X \== P,
nao_membro(X, R).
/*Joins an integer in a sorted list, in a sorted order*/
insere_ordenado(El,[],[El]).
insere_ordenado(El,[P|R],[P|L2]) :- El >= P,
insere_ordenado(El,R,L2).
insere_ordenado(El,[P|R],[El,P|R]) :- El < P.
/*Joins a random integer in a sorted list, in a sorted order*/
junta_novo_aleatorio(L1,INF,SUP,L2) :- random_between(INF, SUP, N),
nao_membro(N,L1),
insere_ordenado(N,L1,L2).
Program:
n_aleatorios(El,INF,SUP,L) :- n_aleatorios(El,INF,SUP,[],0).
n_aleatorios(El,_,_,L,El).
n_aleatorios(El,INF,SUP,L,AC) :- AC =< El,
junta_novo_aleatorio(L,INF,SUP,L2),
AC_num is AC +1,
n_aleatorios(El,INF,SUP,L2,AC_num).
My output:
?- n_aleatorios(3, 1, 5, Lst).
true ;
false.
Expected output (for example):
?- n_aleatorios(3, 1, 5, Lst).
Lst = [2,3,5]
Really any help would be appreciated.
Corrected code:
One slight improvement here:
nao_membro(X, [P | R]) :-
X =\= P,
nao_membro(X, R).
Use "arithmetic non-equivalence" =\= instead of "structural non-equivalence" \== if we can be assured that neither X nor P are fresh. Indeed, here they are always integers.
Fixed code:
junta_novo_aleatorio(L1,INF,SUP,L2) :-
random_between(INF, SUP, N),
nao_membro(N,L1),
insere_ordenado(N,L1,L2).
n_aleatorios(Count,INF,SUP,L) :-
n_aleatorios(Count,INF,SUP,[],L).
n_aleatorios(Count,_,_,L,L) :-
length(L,Count),!.
n_aleatorios(Count,INF,SUP,Lin,Lout) :-
length(Lin,Len), Len<Count,!,
junta_novo_aleatorio(Lin,INF,SUP,Lmid),
n_aleatorios(Count,INF,SUP,Lmid,Lout).
Problems are due to the fact that the list to be constructed is not "threaded" properly between the predicates calls. Here, Lin is the "input list". It is where information "flows into" the predicate. Lin is is used to construct a new list Lout, which appears again the in the predicate argument, but now information "flows out" of the predicate back to the caller.
I have also eliminated the counter, which is already implicit in the length of Lin.
Note that selection between the two clauses of n_aleatorios/5 occurs via a guard followed by a "cut" which commits to the execution path. The second "cut" is actually not needed, because there is no clause below, but makes things clear:
n_aleatorios(Count,_,_,L,L) :-
length(L,Count),!,...(we will never try any clause below)..
n_aleatorios(Count,INF,SUP,Lin,Lout) :-
length(Lin,Len), Len<Count,!,....(we will never try any clause below)..
Related
So I'm making a predicate called removeN(List1, N, List2). It should basically function like this:
removeN([o, o, o, o], 3, List2).
List2 = [o].
The first argument is a list with a number of the same members ([o, o, o] or [x, x, x]). The second argument is the number of members you wanna remove, and the third argument is the list with the removed members.
How should I go about this, I was thinking about using length of some sort.
Thanks in advance.
Another approach would be to use append/3 and length/2:
remove_n(List, N, ShorterList) :-
length(Prefix, N),
append(Prefix, ShorterList, List).
Think about what the predicate should describe. It's a relation between a list, a number and a list that is either equal to the first or is missing the specified number of the first elements. Let's pick a descriptive name for it, say list_n_removed/3. Since you want a number of identical elements to be removed, let's keep the head of the list for comparison reasons, so list_n_removed/3 is just the calling predicate and another predicate with and additional argument, let's call it list_n_removed_head/4, describes the actual relation:
list_n_removed([X|Xs],N,R) :-
list_n_removed_head([X|Xs],N,R,X).
The predicate list_n_removed_head/4 has to deal with two distinct cases: either N=0, then the first and the third argument are the same list or N>0, then the head of the first list has to be equal to the reference element (4th argument) and the relation has to hold for the tail as well:
list_n_removed_head(L,0,L,_X).
list_n_removed_head([X|Xs],N,R,X) :-
N>0,
N0 is N-1,
list_n_removed_head(Xs,N0,R,X).
Now let's see how it works. Your example query yields the desired result:
?- list_n_removed([o,o,o,o],3,R).
R = [o] ;
false.
If the first three elements are not equal the predicate fails:
?- list_n_removed([o,b,o,o],3,R).
false.
If the length of the list equals N the result is the empty list:
?- list_n_removed([o,o,o],3,R).
R = [].
If the length of the list is smaller than N the predicate fails:
?- list_n_removed([o,o],3,R).
false.
If N=0 the two lists are identical:
?- list_n_removed([o,o,o,o],0,R).
R = [o, o, o, o] ;
false.
If N<0 the predicate fails:
?- list_n_removed([o,o,o,o],-1,R).
false.
The predicate can be used in the other direction as well:
?- list_n_removed(L,0,[o]).
L = [o] ;
false.
?- list_n_removed(L,3,[o]).
L = [_G275, _G275, _G275, o] ;
false.
However, if the second argument is variable:
?- list_n_removed([o,o,o,o],N,[o]).
ERROR: >/2: Arguments are not sufficiently instantiated
This can be avoided by using CLP(FD). Consider the following changes:
:- use_module(library(clpfd)). % <- new
list_n_removed([X|Xs],N,R) :-
list_n_removed_head([X|Xs],N,R,X).
list_n_removed_head(L,0,L,_X).
list_n_removed_head([X|Xs],N,R,X) :-
N #> 0, % <- change
N0 #= N-1, % <- change
list_n_removed_head(Xs,N0,R,X).
Now the above query delivers the expected result:
?- list_n_removed([o,o,o,o],N,[o]).
N = 3 ;
false.
As does the most general query:
?- list_n_removed(L,N,R).
L = R, R = [_G653|_G654],
N = 0 ;
L = [_G653|R],
N = 1 ;
L = [_G26, _G26|R],
N = 2 ;
L = [_G26, _G26, _G26|R],
N = 3 ;
.
.
.
The other queries above yield the same answers with the CLP(FD) version.
Alternative solution using foldl/4:
remove_step(N, _Item, Idx:Tail, IdxPlusOne:Tail) :-
Idx < N, succ(Idx, IdxPlusOne).
remove_step(N, Item, Idx:Tail, IdxPlusOne:NewTail) :-
Idx >= N, succ(Idx, IdxPlusOne),
Tail = [Item|NewTail].
remove_n(List1, N, List2) :-
foldl(remove_step(N), List1, 0:List2, _:[]).
The idea here is to go through the list while tracking index of current element. While element index is below specified number N we essentially do nothing. After index becomes equal to N, we start building output list by appending all remaining elements from source list.
Not effective, but you still might be interested in the solution, as it demonstrates usage of a very powerful foldl predicate, which can be used to solve wide range of list processing problems.
Counting down should work fine
removeN([],K,[]) :- K>=0.
removeN(X,0,X).
removeN([_|R],K,Y) :- K2 is K-1, removeN(R,K2,Y).
This works for me.
I think this is the easiest way to do this.
trim(L,N,L2). L is the list and N is number of elements.
trim(_,0,[]).
trim([H|T],N,[H|T1]):-N1 is N-1,trim(T,N1,T1).
As a Prolog newbie, I try to define a predicate filter_min/2 which takes two lists to determine if the second list is the same as the first, but with all occurrences of the minimum number removed.
Sample queries with expected results:
?- filter_min([3,2,7,8], N).
N = [3,7,8].
?- filter_min([3,2,7,8], [3,7,8]).
true.
I tried but I always get the same result: false. I don't know what the problem is. I need help!
Here is my code:
filter_min(X,Y) :-
X == [],
write("ERROR: List parameter is empty!"),
!;
min_list(X,Z),
filter(X,Y,Z).
filter([],[],0).
filter([H1|T1],[H2|T2],Z) :-
\+ number(H1),
write("ERROR: List parameter contains a non-number element"),
!;
H1 \= Z -> H2 is H1, filter(T1,T2,Z);
filter(T1,T2,Z).
There are a couple of problems with your code:
filter([],[],0). will not unify when working with any list that does not have 0 as its minimum value, which is not what you want. You want it to unify regardless of the minimum value to end your recursion.
The way you wrote filter([H1|T1],[H2|T2],Z) and its body will make it so that the two lists always have the same number of elements, when in fact the second one should have at least one less.
A correct implementation of filter/3 would be the following:
filter([],[],_).
filter([H1|T1],L2,Z):-
\+ number(H1),
write("ERROR: List parameter contains a non-number element"),
!;
H1 \= Z -> filter(T1,T2,Z), L2 = [H1|T2];
filter(T1,L2,Z).
A bounty was offered...
... for a pure solution that terminates for (certain) cases where neither the length of the first nor of the second argument is known.
Here's a candidate implementation handling integer values, built on clpfd:
:- use_module(library(clpfd)).
filter_min(Xs,Ys) :-
filter_min_picked_gt(Xs,_,false,Ys).
filter_min_picked_gt([] ,_,true ,[]).
filter_min_picked_gt([Z|Xs],M,Picked,[Z|Zs]) :-
Z #> M,
filter_min_picked_gt(Xs,M,Picked,Zs).
filter_min_picked_gt([M|Xs],M,_,Zs) :-
filter_min_picked_gt(Xs,M,true,Zs).
Some sample queries:
?- filter_min([3,2,7,8],[3,7,8]).
true ; false. % correct, but leaves choicepoint
?- filter_min([3,2,7,8],Zs).
Zs = [3,7,8] ; false. % correct, but leaves choicepoint
Now, some queries terminate even though both list lengths are unknown:
?- filter_min([2,1|_],[1|_]).
false. % terminates
?- filter_min([1,2|_],[3,2|_]).
false. % terminates
Note that the implementation doesn't always finitely fail (terminate) in cases that are logically false:
?- filter_min([1,2|_],[2,1|_]). % does _not_ terminate
For a Prolog newbie, better start with the basics. The following works when first argument is fully instantiated, and the second is an uninstantiated variable, computing the result in one pass over the input list.
% remmin( +From, -Result).
% remmin([],[]). % no min elem to remove from empty list
remmin([A|B], R):-
remmin(B, A, [A], [], R). % remove A from B to get R, keeping [A]
% in case a smaller elem will be found
remmin([C|B], A, Rev, Rem, R):-
C > A -> remmin(B, A, [C|Rev], [C|Rem], R) ;
C==A -> remmin(B, A, [C|Rev], Rem, R) ;
C < A -> remmin(B, C, [C|Rev], Rev, R).
remmin([], _, _, Rem, R) :- reverse(Rem, R).
First, we can get the minimum number using the predicate list_minnum/2:
?- list_minnum([3,2,7,8],M).
M = 2.
We can define list_minnum/2 like this:
list_minnum([E|Es],M) :-
V is E,
list_minnum0_minnum(Es,V,M).
list_minnum0_minnum([],M,M).
list_minnum0_minnum([E|Es],M0,M) :-
M1 is min(E,M0),
list_minnum0_minnum(Es,M1,M).
For the sake of completeness, here's the super-similar list_maxnum/2:
list_maxnum([E|Es],M) :-
V is E,
list_maxnum0_maxnum(Es,V,M).
list_maxnum0_maxnum([],M,M).
list_maxnum0_maxnum([E|Es],M0,M) :-
M1 is max(E,M0),
list_maxnum0_maxnum(Es,M1,M).
Next, we use meta-predicate tfilter/3 in tandem with dif/3 to exclude all occurrences of M:
?- M=2, tfilter(dif(M),[2,3,2,7,2,8,2],Xs).
Xs = [3,7,8].
Put the two steps together and define min_excluded/2:
min_excluded(Xs,Ys) :-
list_minnum(Xs,M),
tfilter(dif(M),Xs,Ys).
Let's run some queries!
?- min_excluded([3,2,7,8],Xs).
Xs = [3,7,8].
?- min_excluded([3,2,7,8,2],Xs).
Xs = [3,7,8].
I am completely confused with Prolog. I have two simple predicates that I am trying to figure out, but can't seem to get them right. First I am trying to figure out a variation of a delete predicate where I need to delete all occurrences of a given element from a List1, and see if it is equal to List2.
This is what I tried.
del(S,[P],[P]).
del(S,[S],[]).
del(S,[H1,H2|T],L2) :- H1 = S, del([H2|T],L2).
del(S,[H1,H2|T],[H1,T2]) :- H1 \= S, del([H2|T],T2).
The other predicate takes two lists and takes all occurrences of the element 2 in List1 and replaces it with 4 and sees if List1 is the same as List2. To make is simplier, there's no sublists.
These are what I have tried.
change([],[]).
change([H1,H2|T],L) :- H1 = 2, append([2],L), change([H2|T],L).
change([H1,H2|T],L) :- H1 \= 2, append(H1,L), change([H2|T],L).
I would like to know what errors I have made and perhaps an explanation of how these terms are suppose to work. Thank you.
Examination of del/3
(1) del(S,[P],[P]).
(2) del(S,[S],[]).
(3) del(S,[H1,H2|T],L2) :- H1 = S, del([H2|T],L2).
(4) del(S,[H1,H2|T],[H1,T2]) :- H1 \= S, del([H2|T],T2).
(1) This rule says that the list [P] is the list [P] with the element S removed. The problem with this rule is that S and P could be the same value, and so the rule isn't always true. If you wanted this to be true all the time (be a valid rule), you'd need to stipulate that S and P can't be instantiated to the same value:
del(S, [P], [P]) :- S \= P.
That's assuming we even need this rule, but we'll leave it here for the time being as, in this new state, it's correct.
(2) This rule says that [] is the list [S] with the element S removed. That is true.
(3) This rule says that L2 is the list [H1,H2|T] with S removed if H1 and S are unified and L2 is [H2|T] with S removed. BUT, there's a missing argument. You have del([H2|T], L2) but the del/3 is defined to take 3 arguments. We'll assume you meant S is the first argument (S is still what's being removed), so del(S, [H2|T], L2). The newly repaired rule appears to be logical.
(4) This rule says that *[H1,T2] is the list [H1,H2|T] with the element S removed if H1 is not unifiable with S and T2 is the list [H2|T] with the element S removed. This has the same problem as #3 with the missing argument to del/3 which we'll, again, assume is S, making it, del(S, [H2|T], T2). Another problem is that you have [H1,T2] which is a list of only two elements: H1 and T2. Another typo. This should be [H1|T2]. So the repaired rule seems to make sense now.
Just fixing these couple of careless errors causes your predicate to almost work! It will yield the correct result when the first argument is instantiated:
| ?- del(a, [a,b,c,a,d,a], L).
L = [b,c,d] ? a
no
Also, it can be cleaned up a bit. The H2 isn't really used in the 3rd and 4th clauses. And in the 3rd clause you can instantiate S and H1 in the head of the predicate. So those two become:
(3) del(S, [S|T], L2) :- del(S, T, L2).
(4) del(S, [H|T], [H|T2]) :- H \= S, del(S, T, T2).
The predicate fails on the list being empty. I'm not sure if this is intentional, but you could argue that del(X, [], []) should be true (an empty list results when you remove an element from the empty list). If we include this rule:
(1a) del(_, [], []).
We can now get rid of rules (1) and (2) because (3) and (4) will take care of them and recurse down to rule (1a).
Additionally, the rules still fail on this case:
| ?- del(X, [a,b,c], [a,c]).
no
It would be nice if this burped out, X = b. The problem is in clause (4) where we check H \= S which means H and S are not unifiable. This expression works against us if S is a variable because then H \= S will always fail (since S could indeed be unified to H). So we replace it with dif(H,S) to check if these terms are the same.
Some Prologs don't offer dif/2, in which case, you could substitute \== for this solution (H \== S). Our resulting rule set is:
(1) del(_, [], []).
(2) del(S, [S|T], L2) :- del(S, T, L2).
(3) del(S, [H|T], [H|T2]) :- dif(H, S), del(S, T, T2).
Let's "reread" these rules:
Any element removed from the empty list is the empty list.
The list [S|T] with the element S removed is the list L2 if L2 is the list T with the element S removed.
The list [H|T2] is the list [H|T] with the element S removed if S is different than H and T2 is the list T with the element S removed.
That looks a lot simpler although just a couple of simple reworks away from the original. It will now produce this result, which is nice:
| ?- del1(X, [a,b,c,b,d], [a,c,d]).
X = b ? ;
(1 ms) no
Examination of change/2
(1) change([],[]).
(2) change([H1,H2|T],L) :- H1 = 2, append([2],L), change([H2|T],L).
(3) change([H1,H2|T],L) :- H1 \= 2, append(H1,L), change([H2|T],L).
(1) This rule says if we change all the 2's in the empty list, we get the empty list. Sounds good.
(2) This rule says if I take the list [H1,H2|T] and I change the 2's to 4's, I get L if H1 is 2 and I append L to [2] (which will always fail since [2] is not variable - see the online docs for append/2 and append/3! append/2 is for appending lists of lists, for example) and L is [H2|T] with 2's changed to 4's. Already this isn't making any logical sense at all. If I'm changing 2's to 4's why am I appending something to [2]? We need to lose the append and simply unify L with [4|T2] where T2 is [H2|T] with its 2's changed to 4's. Or in other words:
(2) change([H1,H2|T], L) :- H1 = 2, L = [4|T2], change([H2|T], T2).
This can be simplified further using the method of incorporating the unification in the head of the clause as above. Also note, again, we have no real use for making H2 visible here. So [H2|T] can just be a T:
(2) change([2|T], [4|T2]) :- change(T, T2).
So we use a 4 instead of a 2 if there's a 2, then process the tails.
(3) This rule has the same problem (2) does with the append/2 query. We can follow the same pattern of what we did with rule (2), and fix the non-equal check:
(3) change([H|T], [H|T2]) :- H \== 2, change(T, T2).
So we just carry over the element H if it's not a 2, then process the tails. The complete change predicate looks like:
(1) change([], []).
(2) change([2|T], [4|T2]) :- change(T, T2).
(3) change([H|T], [H|T2]) :- H \== 2, change(T, T2).
Rereading the rules, seeing that they make logical sense:
Changing 2's to 4's in the empty list is the empty list.
Changing 2's to 4's in the list [2|T] is the list [4|T2] if T2 is the list T with the 2's changed to 4's.
Changing 2's to 4's in the list [H|T] is the list [H|T2] if H is not 2 and T2 is the list T with the 2's changed to 4's.
Lets start with the easiest of both: change/2. Both lists are of same length, and they are essentially the same - safe for occurrences of 2 which should be replaced by 4. So lets first defined a relation for such a pair:
exch(2, 4).
exch(X, X) :-
dif(X, 2).
You might optimize this for reasons of performance to
exch(A, B) :-
( A == 2 -> B = 4
; A \= 2 -> A = B
; A = 2, B = 4
; A = B, dif(A,2)
).
Now, your actual definition is
change(Xs, Ys) :-
maplist(exch, Xs, Ys).
or more verbosely:
change([], []).
change([A|As], [B|Bs]) :-
exch(A, B),
change(As, Bs).
To compare this definition with other definitions proposed, consider the following query:
?- change([X],[Y]).
X = 2, Y = 4
; X = Y, dif(Y, 2).
del(E, Xs, Ys) :-
tfilter(dif_truth, Xs, Ys).
dif_truth(X, Y, true) :-
dif(X, Y).
dif_truth(X, X, false).
tfilter( _, [], []).
tfilter(CT, [E|Es], Fs0) :-
call(CT,E,Truth),
( Truth = false,
Fs0 = Fs
; Truth = true,
Fs0 = [E|Fs]
),
tfilter(CT, Es, Fs).
Further uses
I need some help with three prolog predicates for checking and manipulating lists. I'm new to prolog and any help would be much appreciated.
The three predicates are:
double_up(+List1, -List2) is true when List2 has each element of List1 twice. The query double_up([a,b,c],X) should give X=[a,a,b,b,c,c]. The order of the elements in the output list does not matter.
pivot(+List1, +Pivot, -Smaller, -GreaterEq) is true when Smaller is the list of numbers in List1 smaller than Pivot, and GreaterEq is the list of numbers in List1 bigger than or equal to Pivot.
fancy_replace(+List, +Takeout,+Putin, -NewList, -Count) is true when NewList is the same list as the input List, but where each Takeout element in the list is replaced with the Putin element. Count should be the number of Takeouts that got replaced. For example, the query fancy_replace([9,10,1,9,2],9,0, X, C) should give X = [0,10,1,0,2] and C = 2. The order of the elements in the output list does not matter.
The simpler pattern to process lists in Prolog imposes a recursive predicate with 2 arguments, matching - conventionally - input and output data, and a base case, stopping the recursion, matching the empty list. Then
double_up([X|Xs], [X,X|Ys]) :- double_up(Xs, Ys).
double_up([], []).
This predicate it's a bit more general than what's required, because it works also in mode double_up(-List1, +List2). For instance
?- double_up(L,[1,1,2,2]).
L = [1, 2].
To restrict its mode as required, I think it's necessary to uselessly complicate the code, moving that clean loop in a service predicate, and leaving double_up just to test the arguments:
double_up(I, O) :- is_list(I), var(O), double_up_(I, O).
double_up_([X|Xs], [X,X|Ys]) :- double_up_(Xs, Ys).
double_up_([], []).
pivot/4 could be 'one-liner' in SWI-Prolog:
pivot(List1, Pivot, Smaller, GreaterEq) :-
partition(>(Pivot), List1, Smaller, GreaterEq).
like partition, foldl from library(apply) it's an easy inplementation of the last required predicate:
fancy_replace(List, Takeout, Putin, NewList, Count) :-
foldl(swap_n_count(Takeout, Putin), List, NewList, 0, Count).
swap_n_count(Takeout, Putin, L, N, C0, C) :-
( L == Takeout
-> N = Putin, C is C0 + 1
; N = L, C = C0
).
to be honest, i hate prolog... even though it is fun and easy after you learn it
i think this is a good reference as I was having trouble understanding how prolog works couple weeks ago.
what does the follow prolog codes do?
anyway.. this is the answer for your first problem; Hopefully you could solve the rest yourself :D
double([]).
double([H|[]], [H,H|[]]).
double([H|T],[H,H|T1]):- double(T, T1).
btw, this might not the only solution...but it works
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)
The predicate unique/3 generates all lists of length up to MaxLen consisting of symbols from Symbols. The generated lists are stored in L, once at a time.
unique(MaxLen, Symbols, L) :-
between(0, MaxLen, Len),
length(L, Len),
unique(Symbols, L).
The helper predicate for generating the lists.
unique(_, []).
unique(Set, [H|R]) :-
select(H, Set, ReducedSet),
unique(ReducedSet, R).
A simple program for demonstrating the above predicate:
main :-
unique(5, [2,3], L),
write(L), nl, fail.
Here's one way, using SWI-PROLOG built-ins for atomic_list_concat/2, atom_number/2 and select/3. Firstly, the entry point refers to an implementation using an initially empty accumulator:
numberFromList(L, N) :-
numberFromList(L, [], N).
The predicate numberFromList/3 either accumulates digits (unchecked) from the list, or not, leaving choicepoints:
numberFromList([_|Cs], Acc, N) :-
numberFromList(Cs, Acc, N).
numberFromList([C|Cs], Acc, N) :-
numberFromList(Cs, [C|Acc], N).
The final clause of numberFromList/3 permutes the accumulated list of digits and concatenates them into an atom, which is then converted to a number as required:
numberFromList([], [C|Cs], N) :-
permute([C|Cs], PermutedAcc),
atomic_list_concat(PermutedAcc, AN),
atom_number(AN, N).
Sometimes permute/2 (as defined manually below) may be available as a built-in, such as permutation/2. Here is a manual definition using select/3:
permute([], []).
permute([E|Es], [E0|PL]) :-
select(E0, [E|Es], Rem),
permute(Rem, PL).
If you want a list of all the results and don't want numberFromList/2 to backtrack itself, you could wrap the call to numberFromList/3 (with the empty accumulator in the first clause of numberFromList/2) in a findall/3 call.