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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T00:38:07+00:00 2026-06-09T00:38:07+00:00

I have the following task: Write a method that will add two polynoms. I.e

  • 0

I have the following task:

Write a method that will add two polynoms. I.e 0+2*x^3 and 0+1*x^3+2*x^4 will give 0+3*x^3+2*x^4.

I also wrote the following code:

add_poly(+A1*x^B1+P1,+A2*x^B2+P2,+A3*x^B3+P3):-
    (
       B1=B2,
       B3 = B2,
       A3 is A1+A2,
       add_poly(P1,P2,P3)
    ;
       B1<B2,
       B3=B1,
       A3=A1,
       add_poly(P1,+A2*x^B2+P2,P3)
    ;
       B1>B2,
       B3=B2,
       A3=A2,
       add_poly(+A1*x^B1+P1,P2,P3)
    ).
add_poly(X+P1,Y+P2,Z+P3):-
    Z is X+Y,
    add_poly(P1,P2,P3).

My problem is that I don’t know how to stop. I would like to stop when one the arguments is null and than to append the second argument to the third one. But how can I check that they are null?
Thanks.

  • 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-09T00:38:09+00:00Added an answer on June 9, 2026 at 12:38 am

    Several remarks:

    Try to avoid disjunctions (;)/2 in the beginning. They need special indentation to be readable. And they make reading a single rule more complex — think of all the extra (=)/2 goals you have to write and keep track of.


    Then, I am not sure what you can assume about your polynomials. Can you assume they are written in canonical form?


    And for your program: Consider the head of your first rule:

    add_poly(+A1*x^B1+P1,+A2*x^B2+P2,+A3*x^B3+P3):-
    

    I will generalize away some of the arguments:

    add_poly(+A1*x^B1+P1,_,_):-
    

    and some of the subterms:

     add_poly(+_+_,_,_):-
    

    This corresponds to:

    add_poly(+(+(_),_),_,_) :-
    

    Not sure you like this.

    So this rule applies only to terms starting with a prefix + followed by an infix +. At least your sample data did not contain a prefix +.

    Also, please remark that the +-operator is left associative. That means that 1+2+3+4 associates to the left:

    ?- write_canonical(1+2+3+4).
    +(+(+(1,2),3),4)
    

    So if you have a term 0+3*x^3+2*x^4 the first thing you “see” is _+2*x^4. The terms on the left are nested deeper.


    For your actual question (how to stop) – you will have to test explicitly that the leftmost subterm is an integer, use integer/1 – or maybe a term (*)/2 (that depends on your assumptions).

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

Sidebar

Related Questions

I have the following task: Write a program that asks for a number and
I am charged with the following task in a Rails project. Clients will have
i have the following code to disable task manager of windows xp but it
I need a method to perform the following task, suppose if I have a
I have the following method that works fine. However, I think it is ugly.
I had to write a method that does the following: There is a DataSet
I have two XML examples that I want to write a schema for: Example
I've been given the following task: An external source will write a name, datetime
I have the following task: there are a simple table from mysql database: <html>
I have the following task, which because of the combination of DestinationFiles and DestionationFolder

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.