Re: Sublists question

From: no-spam (no-spam_at_sonic.net)
Date: 03/28/04


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

--


Relevant Pages

  • Re: Why? Whaaa haaaaa whyyyyy?
    ... recoding back to lossless, but it was my impression you would not get ... the same md5's for shn's even if you're dealing with the same data. ...
    (rec.music.gdead)
  • Re: Is this kind of auction allowed?
    ... Benjamin Gawert wrote: ... > For me I have the impression that this is another attempt to rip-off people. ... > People dealing on ebay usually are aware that most bidders simply don't read ...
    (comp.sys.sgi.misc)
  • Re: Unexpected Trip Coming Up
    ... that never materialize? ... but it's not very becoming of them. ... left with the impression your dealing with a gaggle of idiots. ...
    (alt.vacation.las-vegas)
  • Re: For those of you who are listening to LQs nonsense
    ... confidence in dealing with sugar issues at this place in the T2 ... spectrum. ... My impression from my recent dealings with them is that if it isn't ... do a better job of id'ing prediabetes in patients" most docs still ...
    (alt.support.diabetes)
  • Re: mv is too smart
    ... I actually like the filename completion solution best. ... I did some experimenting and found that Esc-\ ... I'll be able to tell what I'm dealing with. ... self-training though. ...
    (comp.unix.shell)