Enumerating subscriptions with a prologue

I'm trying to write a prologue program that behaves like this: it lists all possible sequential ones (I'm not sure if this is the correct phrase, ie [a, c] is not a sub-list of the sub-edge [a, b, c]), the length of subscriptions, the distance from the first item in the correct list to the first item in the sublist, and the distance from the last item in the subscriptions to the last item in its own list. So the input / output looks like this ideally:

| ?- sublist([a,b], SubList, Before, Len, After).
    SubList= [], After = 2, Before = 0, Len = 0 ? ;
    SubList= [a], After = 1, Before = 0, Len = 1 ? ;
    SubList= [a,b], After = 0, Before = 0, Len = 2 ? ;
    SubList= [], After = 1, Before = 1, Len = 0 ? ;
    SubList= [b], After = 0, Before = 1, Len = 1 ? ;
    SubList= [], After = 0, Before = 2, Len = 0 ? ;

      

So far, I was able to correctly output the sublists and output their lengths, for example:

sublist(L,S,Len) :-
    append(_,L2,L), %getting sublist
    append(S,_,L2), %getting sublist
    length(S,Len).  %getting length of sublist

      

but I'm having a hard time figuring out how I can keep track of "Before" and "After". This seemingly (possibly sneaky?) Way of doing this is to start B at -1 and increment "to" by 1 every time a [] -digit sublist is encountered (since meeting with [] means there was a head and now we start from the very tail), and once you have "Before", "After" will just be ([input list length] - "Before" - [subscription length]), unfortunately I'm completely lost, how could I increase "To". Any thoughts? Thank!:)

+3


source to share


1 answer


Until now, I was able to correctly output sublists and output their lengths, eg [...]

You almost got it.



Just bind the prefix and suffix to variables and make sure the prefix Before

length and the suffix length are After

:

sublist_lengths(L, SubList, Before, Len, After) :-
    append(PrefSub, Suffix, L),
    append(Prefix, SubList, PrefSub),
    length(SubList, Len),
    length(Prefix, Before),
    length(Suffix, After).

      

+3


source







All Articles