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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:25:53+00:00 2026-05-27T20:25:53+00:00

I have written the following code in SWI-Prolog: :- dynamic state_a/1 . :- dynamic

  • 0

I have written the following code in SWI-Prolog:

:- dynamic state_a/1 .
 :- dynamic state_b/1 .
 :- dynamic state_c/1 .
 state_a([1,2,3,4,5,0]).
 state_b([0]).
 chop(LIST,HEAD,TAIL) :- LIST=[HEAD|TAIL].
 move_ab :- !,state_a(X),
            chop(X,Ha,Ba),
            Ha>0,
            state_b(Y),
            chop(Y,Hb,Bb),!,
            (Ha<Hb ; Hb =:= 0),
            asserta(state_a(Ba)),asserta(state_b([Ha|Y])),
            retract(state_a(X)), retract(state_b(Y));
            write('Wrong Move.Choose Another').

There are 2 OR(;) in my code. When I ask move_ab in Prolog for the first time all the conditions before the second OR are true so i get the answer true from Prolog.
But the second time I ask move_ab in Prolog I get just the answer false.
I don’t know how this can happen. Some of the conditions before the second OR are not true so Prolog should check the condition after the second OR and write me the message Wrong Move.Choose Another..
I tried using () to group the conditions but I still get the same message.
Any idea about what is going on? By the way I am a newb in Prolog, just started 2 days ago 🙂

  • 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-27T20:25:54+00:00Added an answer on May 27, 2026 at 8:25 pm

    The problem lies in the use of cut (!/0) before the conditions are properly tested. Cut removes choice points. Here it means that the choices are removed before you even test anything. So if the tests fail, everything fails !

    BTW, manipulating the database is maybe not the best idea there. To represent states, you could use global variables as follows :

    :- nb_setval(state_a, [1,2,3,4,5,0]).
    :- nb_setval(state_b, [0]).
    
    move_ab :-
        nb_getval(state_a, [Ha|Ta]),
        Ha > 0,
        nb_getval(state_b, [Hb|Tb]),
        (Ha < Hb ; Hb =:= 0),
        nb_setval(state_a, Ta),
        nb_setval(state_b, [Ha, Hb|Tb]),
        !
        ;
        write('Wrong Move.Choose Another').
    

    A general advice given to beginners in prolog is to stay away from database manipulation if possible, as often problems are solvable without it. Here though it could be justified, but global variables will be faster and easier to manipulate.

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

Sidebar

Related Questions

I have written a following code to get just the file name without extension
I have written the following code choice /m Do you want to add another
I have written the following code. But it is removing only &nbsp; not <br>
i have written the following code: As you can see there is a for
I have written the following IronPython code: import clr clr.AddReference(System.Drawing) from System import *
I have written my code in following way in cocos2d. id actionTo = [CCFadeOut
In physics library written in C# I have the following code: (in ContactManager.cs) public
Have written the following code: m_selectCategoryTableWidget = new QTableWidget; m_selectCategoryTableWidget->setRowCount(0); m_selectCategoryTableWidget->setColumnCount(2); m_selectCategoryTableWidget->setHorizontalHeaderLabels(QStringList()<<tr(Category)<<tr(Number of items));
I have written following PHP code: $input=menu=1&type=0&; print $input.<hr>.ereg_replace('/&/', ':::', $input); After running above
I have written the following code in my Rails app to generate XML. I

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.