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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:44:03+00:00 2026-06-15T13:44:03+00:00

I want to create a list with coefficients from a nested structure in prolog.

  • 0

I want to create a list with coefficients from a nested structure in prolog.

For example: (structure –> return value)

item(koeffizient(2), exponent(2), item(koeffizient(3), exponent(3))) –> [0,0,2,3]

item(koeffizient(5), exponent(0), item(koeffizient(1), exponent(1), item(koeffizient(3), exponent(3)))) –> [5,1,0,3]

item(koeffizient(5), exponent(0)) –> [5]

item(koeffizient(5), exponent(0), item(koeffizient(2), exponent(2))) –> [5,0,2]

How can i do this in a recursive way? Actually i have no idea how i can do this.

Thank’s for any help you can give me =)

  • 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-15T13:44:04+00:00Added an answer on June 15, 2026 at 1:44 pm

    First note that is not a good idea to use a recursive structure the way you have it defined, as it differentiates the last item from the others.

    You can do it greedily, assuming that your structure is ordered by the exponents (as your examples show):

    structure(Struct, Exps):-
      structure(Struct, 0, Exps).
    
    structure(item(koeffizient(Coeff), exponent(Exp)), Exp, [Coeff]).
    structure(item(koeffizient(Coeff), exponent(MExp)), Exp, [0|Tail]):-
      MExp > Exp,
      succ(Exp, NExp),
      structure(item(koeffizient(Coeff), exponent(MExp)), NExp, Tail).
    structure(item(koeffizient(Coeff), exponent(Exp), StructTail), Exp, [Coeff|Tail]):-
      succ(Exp, NExp),
      structure(StructTail, NExp, Tail).
    structure(item(koeffizient(Coeff), exponent(MExp), StructTail), Exp, [0|Tail]):-
      MExp > Exp,
      succ(Exp, NExp),
      structure(item(koeffizient(Coeff), exponent(MExp), StructTail), NExp, Tail).
    

    Procedure structure/2 just calls structure/3 with exponent 0.

    The first clause of structure/3 is the base case. It returns the coefficient for the last exponent.

    The second clause matches the base case when the current exponent does not match. So in this case we return a zero as the next coefficient, increments the current exponent and applies recursion.

    The third clause matches the recursive case when the current exponent matches the current exponent of the structure. So it returns the corresponding coefficient, increments the current exponent and applies recursion again.

    The last clause is like the second clause but applies to input structures that still have more items left (apart from the current one).

    If, as your comments say the input is not ordered then it might be better to transform your input to a list of ordered tuples of the form Exp-Coeff.

    With such list, your structure/2 procedure remains the same and you can symplify procedure/3 to this one:

    structure([], _, []).
    structure([Exp-Coeff|Tail], Exp, [Coeff|TailCoeffs]):-
      succ(Exp, NExp),
      structure(Tail, NExp, TailCoeffs).
    structure([MExp-Coeff|Tail], Exp, [0|TailCoeffs]):-
      MExp > Exp,
      succ(Exp, NExp),
      structure([MExp-Coeff|Tail], NExp, TailCoeffs).
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create a list within a list item on jQuery mobile here's
I`v come across a need where I want to create multiple list items from
I want to create a list of lambda objects from a list of constants
I want to create a list of integers from 1 to n . I
I want to create a list of word pairs, separated by tabs, from a
I want to create a list view which contain TextViews that are built from
I want to create collapsible list (jquery mobile) by looping from javascript. From that
I want to create a list from one to ten. Each time it loops
I want to create a list from the facts. And the list should contains
I am new in ubuntu 12.04 LTS . I want to create list file

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.