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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T19:26:12+00:00 2026-06-03T19:26:12+00:00

I am struggling to get my head around Prolog lists. Here’s my problem: I

  • 0

I am struggling to get my head around Prolog lists.

Here’s my problem: I need to take in a list and two variables and store the odd elements in list A and the even elements in list B.

I got this but its not giving the results im looking for

store(X, [], []).
store([X,Y|Z],[X|_],[Y|_]):-store(Z,X,Y).

Result should be:

where ?- store ([a,b,c,1,2,3], A, B).
         A = [b,1,3].
         B = [a,c,2].
  • 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-06-03T19:26:14+00:00Added an answer on June 3, 2026 at 7:26 pm

    to build additional lists from a list argument do a recursive visit of it, storing elements where appropriate. See if you can complete this snippet

    odd_even([], [], []).
    odd_even([N|Ns], [N|Odds], Evens) :-
       odd(N),
       !, odd_even(Ns, Odds, Evens).
    % here another odd_even for evens
    
    odd(N) :-
      ... % use rem/2 to find the integer remainder of N is 1
    

    edit: to move elements from even places in a list and elements at odd places in another (usually called a split), the visit just places them on two additional arguments: we need one more termination rule, because now we consider two arguments together from the list to be splitted.

    split([], [], []).
    split([X], [], [X]).
    split([X,Y|R], [X|Xs], [Y|Ys]) :-
       split(R,Xs,Ys).
    

    there is a case to consider: if odd vs even must be ‘counted’ from the end of list.
    Then we should swap (eventually) the lists when done…That would require to add 2 more arguments to the recursive visit, or count the num.of.elements to decide beforehand where to aplce them…

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

Sidebar

Related Questions

I'm struggling to get my head around how to do the following: I have
I am struggling to get my head around how to scale any sort of
I am struggling to get my head around operator overloading. In this case the
I'm really struggling to get my head around Drupal Form API...actually Drupal as a
i am seriously struggling to get my head around regex. I have a sring
Ok so i've been struggling to get my head around this for a few
I'm a R user from way back but struggling to get my head around
I'm struggling to get my head around how to implement UJS in Rails (specifically,
Struggling to get my head around these MD Arrays so hoping someone can help.
I'm struggling to get my head around the differences between the various places you

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.