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 6905437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:11:00+00:00 2026-05-27T08:11:00+00:00

I am trying to compare the lists. Given function(List1,List2) and List1 has length N

  • 0

I am trying to compare the lists. Given function(List1,List2) and List1 has length N and List 2 has length M and N>M.

I want to check if any permutation of List2 happens to be the first M characters of List1.

eg,

predicate([a,c,b,d,e],[a,b,c,d]).

should be true and

predicate([a,c,b,e,d],[a,b,c,d]).

should be false.

Thank you.

  • 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-27T08:11:00+00:00Added an answer on May 27, 2026 at 8:11 am

    Using the permutation/2 and prefix/2 predicates you could write something such as :

    has_prefix_perm(List1, List2) :-
        permutation(List2, Permutation),
        prefix(Permutation, List1),
        !.
    

    As a side note and to quote swi-prolog manual :

    Note that a list of length N has N! permutations and unbounded permutation generation becomes prohibitively expensive, even for rather short lists (10! = 3,628,800).

    So I’d take care not to call has_prefix_perm/2 with a too lengthy second list, especially if it happens not to be a prefix modulo permutation, since all the cases will be tested.

    Another way would be to test if List1 items are members of List2 until List2 is empty, that way you know that a permutation exists :

    has_prefix_perm(_, []) :- !.
    has_prefix_perm([Head1|List1], List2) :-
        once(select(Head1, List2, Rest)),
        has_prefix_perm(List1, Rest).
    

    Written like that, I wouldn’t use it on non ground lists, but seeing your OP I didn’t search further…

    Another way would be to check if List1 reduced to the length of List2 is a permutation of List2 :

    has_prefix_perm(List1, List2) :-
        length(List2, L),
        length(LittleL1, L),
        append(LittleL1, _, List1),
        permutation(LittleL1, List2),
        !.
    

    Another way would be to… I guess there are lots of ways to do that, just pick one that isn’t horrible complexity wise and fits your style ! 🙂

    I’d go with the last one personally.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a function in three classes that will compare two lists
I'm trying to compare two lists by length and set the output to true
I am trying to compare two files. I will list the two file content:
F# function Problem: given a list of items e.g.: [5;10;2;53;4] and a Search Index,
I'm trying to find out how I can compare 2 lists of RPMS (Currently
I am trying to compare two lists of string in python. Some of the
I'm trying to move items in my list but when I compare against the
lest say I have two lists List1: Tom, Frank, Lacey List2: Frank, Tom what
I am trying to compare a list AAA that contain Date with a range
I'm trying to compare a list of strings compiled together against a master list

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.