Re: Sublists question
From: no-spam (no-spam_at_sonic.net)
Date: 03/28/04
- Next message: zeus: "Re: Sublists question"
- Previous message: alex: "Re: How to parse expressions with parenthesis"
- In reply to: Bart Demoen: "Re: Sublists question"
- Next in thread: no-spam: "Re: Sublists question"
- Reply: no-spam: "Re: Sublists question"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sun, 28 Mar 2004 05:24:50 GMT
Bart Demoen wrote:
> billh wrote:
>
>> Not too bad, taking into consideration the fact that I am using a
>> 400MHz AMD k6-3 cpu. I was frankly surprised to get any answers at
>> all, and I look forward to seeing how well a "closed form" method of
>> dealing with the general case performs in comparison.
>
>
> Would you please be so kind as to provide us with the definitions of
> sublist_i_k and is_sublist that you used.
> I can write my own version of these pr4edicates, but I'd rather use yours
> before I start reporting on a few "closed form" methods.
>
> Cheers
>
> Bart Demoen
Here is the version of sublist_i_k that I used yesterday:
sublist_i_k(I,K,Sublist_I_K) :-
K > 1,
K1 is K - 1,
sublists_i_k(I,K1,[I],Sublist_I_K).
% example: sublist_i_k(2,3,L) => L=[2_,_,2,_,_,2]
sublists_i_k(I,0,In,In).
sublists_i_k(I,K,In,Out) :-
K > 0,
sublist_i(I,I,In,Next),
K1 is K - 1,
sublists_i_k(I,K1,Next,Out).
sublist_i(0,I,In,[I|In]).
sublist_i(J,I,In,Out) :-
J > 0,
J1 is J - 1,
sublist_i(J1,I,[_|In],Out).
and here is the version of is_sublist that I used:
is_sublist(S,L) :-
append(A,SB,L),
append(S,B,SB).
I have since discovered that the version of sublist_i given above
throws the PDC Prolog (v3.21) flow-analyzer into an endless loop, so
today I am experimenting with the following version of sublist_i_k
(which doesn't):
sublist_i_k(I,K,Sublist_I_K) :-
K > 1,
K1 is K - 1,
sublists_i_k(I,K1,[],Sublist_I_K).
% example: sublist_i_k(2,3,L) => L=[2_,_,2,_,_,2]
sublists_i_k(I,0,In,[I|In]).
sublists_i_k(I,K,In,Out) :-
K > 0,
sublist_i(I,I,In,Next),
K1 is K - 1,
sublists_i_k(I,K1,Next,Out).
sublist_i(0,I,In,[I|In]).
sublist_i(J,I,In,[_|Out]) :-
J > 0,
J1 is J - 1,
sublist_i(J1,I,In,Out).
p.s. I hope I didn't give anyone the impression that my program printed
out all 26,880 answers to the case n=17, k=3 while I was writing my
commemt. That took a little longer. (Just kidding!)
Bill
--
- Next message: zeus: "Re: Sublists question"
- Previous message: alex: "Re: How to parse expressions with parenthesis"
- In reply to: Bart Demoen: "Re: Sublists question"
- Next in thread: no-spam: "Re: Sublists question"
- Reply: no-spam: "Re: Sublists question"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|