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

The Archive Base Latest Questions

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

I have to write a program that will get lists in a list, and

  • 0

I have to write a program that will get lists in a list, and should reverse every second list, then return the result. Something like this:

doStuff([[1,2], [3,4], [5,6], [7,8]], R).

R = [[1,2], [4,3], [5,6], [8,7]]

This is what I have so far:

doStuff([],_).
doStuff([X,S|T],R):- reverse(S,Rev), add(X,Rev,R), doStuff(T,R).
doStuff([X|[]], R):- add(X,R,R).

reverse([X|Y],Z,W) :- reverse(Y,[X|Z],W).
reverse([],X,X).

add(X,[],X).
add(X,L,[X,L]).

My problem is that, in the second iteration the add function fails.
I’m also concerned about what will happen when the original list contains only one list at the end.

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

    since you said that add/3 fails after the second iteration i assume that you used trace/0
    anyway, here it is:

    [trace]  ?- doStuff([[1,2], [3,4], [5,6], [7,8]], R).
       Call: (6) doStuff([[1, 2], [3, 4], [5, 6], [7, 8]], _G405) ? creep
       Call: (7) lists:reverse([3, 4], _G496) ? creep
       Exit: (7) lists:reverse([3, 4], [4, 3]) ? creep
       Call: (7) add([1, 2], [4, 3], _G405) ? creep
       Exit: (7) add([1, 2], [4, 3], [[1, 2], [4, 3]]) ? creep
       Call: (7) doStuff([[5, 6], [7, 8]], [[1, 2], [4, 3]]) ? creep
       Call: (8) lists:reverse([7, 8], _G514) ? creep
       Exit: (8) lists:reverse([7, 8], [8, 7]) ? creep
       Call: (8) add([5, 6], [8, 7], [[1, 2], [4, 3]]) ? creep
       Fail: (8) add([5, 6], [8, 7], [[1, 2], [4, 3]]) ? creep
       Redo: (7) doStuff([[5, 6], [7, 8]], [[1, 2], [4, 3]]) ? creep
       Fail: (7) doStuff([[5, 6], [7, 8]], [[1, 2], [4, 3]]) ? creep
       Redo: (6) doStuff([[1, 2], [3, 4], [5, 6], [7, 8]], _G405) ? creep
       Fail: (6) doStuff([[1, 2], [3, 4], [5, 6], [7, 8]], _G405) ? creep
    false.
    

    so why does it fail? it’s because of one of prolog’s main characteristics
    basically, a variable in prolog, once it takes a value, it can never change
    this may sound kinda weird if you used imperative languages so far but consider that exactly the same thing happens in math.
    For example, consider the following equations:

    x+3=4  
    4-x=5
    

    from the first we get that x=1
    and from the second we get that x=-1
    since x cannot be 1 and -1 we say that there is no x to satisfy the equations above;
    not that x is -1 since that’s the value of the second (and last) equation

    exactly the same thing happens in yout program:

    doStuff([X,S|T],R):- reverse(S,Rev), add(X,Rev,R), doStuff(T,R).
    

    so the first time R is assigned a value and the doStuff/2 is call with a value for R.
    obviously, this is not the correct value so doStuff/2 fails

    so how can you fix this? look at the way you wrote [X,S|T] to separate the first two elements that you wanted to process. you only have to do exactly the same with the result:
    write it like [XResult,SResult|Rest]
    of course XResult will be simply X and SResult will be the reverse of S (Rev)
    Rest will be what you get from the recursive call of doStuff/2

    last but not least you should check the clauses that are called in the end.

    you could check some stuff about lists and recursion

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

Sidebar

Related Questions

I have a class assignment where I have to write a program that will
I have to write a program that read from a file that contains the
I have to write a program that sniffs network packets (part1-the simple part). And
I wanted to write a program that test if two files are duplicates (have
I have been assigned wit the task to write a program that takes a
I have a Perl program, that needs to use packages (that I also write).
I need to write a program used internally where different users will have different
I'm needing to write a program (C#) that will allow the user to create
I need to write a program that will take a existing local windows user,
Supposing I have a File f that represents a directory, then f.delete() will only

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.