Re: multisets
- From: Chip Eastham <hardmath@xxxxxxxxx>
- Date: Fri, 22 Feb 2008 17:22:03 -0800 (PST)
On Feb 22, 2:20 pm, Stephan Lukits <Stephan.Luk...@xxxxxxxxxxxxxxxx>
wrote:
Hi,
this time I wonder how the Prolog geeks would write
a predicate which gets multisets of cardinality C
filled with numbers between M and N (inclusive).
(Example: C: 2, M: 1, N 2:
[1, 1]; [1, 2]; [2,2] (because [1, 2] = [2, 1]))
The only solution I can think of is, producing
all combinations and cutting the duplicates:
checkSet([E1,E2|[]]) :-
!, E1 =< E2.
checkSet([E1,E2|R]) :-
E1 =< E2,
checkSet([E2|R]).
multiSets(M, N, 1, S) :-
length(S, 1),
maplist(between(M, N), S).
multiSets(M, N, C, S) :-
length(S, C),
maplist(between(M, N), S),
checkSet(S).
But this seems more expensive as it needs
to be?
Thanks for any suggestions. And even bigger
Thanks for any explications.
best regards
Stephan
I'd modify your approach to produce a monotone
increasing list of length C (containing values
between M and N inclusive).
Untested code follows:
multiset(C,M,N,S) :-
length(S,C),
increasing(M,N,S).
increasing(_,_,[]).
increasing(M,N,[H|T]) :-
between(M,N,H),
increasing(H,N,T).
Alternatively we might eliminate dependence on a
built-in length/2:
multiset(0,_,_,[].
multiset(C,M,N,[H|T]) :-
C > 0,
between(M,N,H),
B is C - 1,
multiset(B,H,N,T).
regards, chip
.
- Follow-Ups:
- [solved] Re: multisets
- From: Stephan Lukits
- [solved] Re: multisets
- References:
- multisets
- From: Stephan Lukits
- multisets
- Prev by Date: multisets
- Next by Date: Re: if then else
- Previous by thread: multisets
- Next by thread: [solved] Re: multisets
- Index(es):
Relevant Pages
|
|