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

  • Home
  • SEARCH
  • 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 1103225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:18:37+00:00 2026-05-17T01:18:37+00:00

I need write a set of clauses that take a list of integer lists

  • 0

I need write a set of clauses that take a list of integer lists and return a single list with all the elements doubled.

For example:

?- double([[1,2],[3]], X).
Yes
X = [2,4,6]

I have a set of clauses called mega_append that return a single list from a list of lists.

For example:

?- mega_append([[1,2],[3]], X).
Yes
X = [1,2,3]

Here is my progress (m_a is short for mega_append):

double([],[]).
double(List,[H1|T1]) :-
    m_a(List,[H2|T2]),
    H1 is 2 * H2,
    double(T2, T1).

I’ll try to explain how I thought it would work. I flatten the first list and split it up into a head and a tail (H2 and T2). I split the second list into a head and a tail (H1 and T1). I check to make sure that H1 (the doubled value) is equal to 2 times H2 (the original value). If it is then I check the rest of the list. Eventually if they all match correctly I should be left with two empty lists which should match the first clause and return yes.

It works when there is only a single value (for example: double([[1]], X)). Can anyone offer any insight into what I am doing wrong? Is my logic or code incorrect?

  • 1 1 Answer
  • 1 View
  • 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-17T01:18:37+00:00Added an answer on May 17, 2026 at 1:18 am

    Your problem is that T2 is a single list so List after the recursive call is not a list of lists.

    To solve this you can first use mega_append to flatten the list and then use an auxiliary predicate to work on the flattened list.
    I.e. the double will look like this:

    double([],[]).
    double(List,X) :-
        m_a(List,FList),
        double_aux(List, FList).
    

    Edit:
    Here is a way to only use one clause since you want to see one.
    I recommend using an auxiliary predicate.

    double([],[]).
    double([[]],[]).
    double(List,[H1|T1]) :-
        mega_append(List,[H2|T2]),
        H1 is 2 * H2,
        double([T2], T1).
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to write a generic procedure that set value for one column or
I need to write a a query that gets a set of information from
I need to write the expression meaning optimize over the parameter set . I
I need to write, in JavaScript (I am using jQuery), a function that is
I need to write a small console app (patch) that turns off the print
I need to write an app that reads a config file with info on
I need to write a python script that makes multiple HTTP requests to the
I need to write a little ruby function that does word wrapping. I have
I need to write a query that will perform a keyword search on a
I find that I need to write the same code in many classes. The

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.