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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:03:18+00:00 2026-05-30T00:03:18+00:00

I am trying to learn more about lists in Prolog, especially lists within a

  • 0

I am trying to learn more about lists in Prolog, especially lists within a list. So I want to write a predicate to determine if the lists in a list are equal. So if I did this

?- equalLists([[a,b,c],[1,2,3],[d,4,e],[5,6]]).
false

So I’m trying to check each list to see if it is has the same length as the previous list. Can someone point me in the right direction?

  • 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-30T00:03:19+00:00Added an answer on May 30, 2026 at 12:03 am

    @Peteris’s solution is unnecessarily complex. You can do it like this:

    equal_lengths([]).
    equal_lengths([[]]).
    equal_lengths([[_|_]]).
    equal_lengths([X,Y|Rest]) :- 
      length(X, Len), 
      length(Y, Len), 
      equal_lengths([Y|Rest]).
    

    Think inductively. I’m asserting that the length of the empty list and a list of one list are “equal” (there are no/just one other to compare to). Then I say if the length of the first item equals the length of the second item, as long as the lengths of the second item and the rest match we are good.

    Note: we’re not explicitly saying that the length of X and Y is the same with some kind of equality test. We’re letting Prolog handle that for us, by simply declaring that the length of X and Y is Len. So if the length of Y doesn’t unify with the length of X, the predicate will fail.

    Reversing the process

    So, to write a predicate to determine if none of the lists have the same length we must observe that this time we will have to keep track of which lengths have been seen so far to check against. We must compare each list’s length to all the preceeding list lengths to determine inequality. So this time our initial case will create the initial list of lengths and defer processing to another predicate like so:

    unequal_lengths(X) :- unequal_lengths(X, []).
    

    Now we again begin with similar base cases:

    unequal_lengths([], _).
    unequal_lengths([[]], _).
    unequal_lengths([[_|_]], _).
    

    Things get interesting when we have an actual list:

    unequal_lengths([X|Rest], Lengths) :-
      length(X, Len),
      \+ member(Len, Lengths),
      unequal_lengths(Rest, [Len|Lengths]).
    

    So we’re calculating the length of this list, then asserting that this is not a length we have seen before with the handy member predicate, then passing this length along with the rest along to the remainder of the list.

    Addendum: unequal w/ forall

    Inspired by the other answers, you can implement unequal_lengths in a higher-order fashion like so:

    unequal_lengths(Lists) :-
      findall(Len, (member(X, Lists), length(X, Len)), Lengths),
      forall(select(Len, Lengths, Remaining), \+ member(Len, Remaining)).
    

    If you think about it, this corresponds quite closely to a formal logic expression of the problem: for every list length, there does not exist an element of the remaining list lengths corresponding to this one.

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

Sidebar

Related Questions

I am trying to learn more about the PHP function sprintf() but php.net did
I am trying to learn more about list containers and how to iterate through
I am trying to learn a little more about the List Collection and am
I have been trying to learn more about lambda expressions lately, and thought of
I am trying to learn more about regular expressions I have one below that
I'm trying to learn more about WPF. I ran through an online tutorial that
I'm trying to learn more about WPF and I've read a bit about Model-View-ViewModel
I've been trying to learn more about using Lamba expression trees and so I
So I'm trying to learn more about lambda expressions. I read this question on
In my quest in trying to learn more about OOP in PHP. I have

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.