Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7649539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T10:59:31+00:00 2026-05-31T10:59:31+00:00

I’m trying to write a prolog program that behaves in the following fashion: it

  • 0

I’m trying to write a prolog program that behaves in the following fashion:
it enumerates all possible consecutive (I’m not sure if that is the proper phrase, i.e. [a,c] is not a sublist of [a,b,c]) sublists, the length of the sublist, the distance from the first element of the proper list to the first element of the sublist and the distance from the last element of the sublist to the last element of the proper list.
So input/output would ideally look something like:

| ?- 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 can output the sublists properly, and output their lengths, like so:

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 trouble figuring out how I might keep track of ‘Before’ and ‘After’.
It seemed like a (perhaps sneaky?) way of doing it would be to start B at -1 and increment ‘Before’ by 1 every time a [ ]-valued sublist is encountered (since encountering [ ] means that the the head has been removed and now we’re starting over with just the tail), and once you have ‘Before’, ‘After’ would just be ([the length of the input list] – ‘Before’ – [the length of the sublist]). Unfortunately, I am at a complete loss as to how I might go about incrementing ‘Before’.
Any thoughts? Thanks so much! 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T10:59:32+00:00Added an answer on May 31, 2026 at 10:59 am

    So far I can output the sublists properly, and output their lengths, like so […]

    You almost got it.

    Just bind the prefix and suffix to variables, and make sure the length of the prefix is Before and the length of the suffix is 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).
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I need to write an AS3 program to search for a certain keyword in
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.