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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:16:42+00:00 2026-05-27T06:16:42+00:00

predicates pathdistance(symbol,symbol,integer). solve(symbol,symbol,integer). clauses pathdistance(a,b,10). pathdistance(b,c,20). pathdistance(c,d,5). pathdistance(d,e,15). pathdistance(a,d,5). pathdistance(c,e,10). solve(X,Z,C):- pathdistance(X,Z,C). solve(X,Z,C):- pathdistance(X,Y,Cost),

  • 0
predicates
          pathdistance(symbol,symbol,integer).
          solve(symbol,symbol,integer).

clauses
   pathdistance(a,b,10). 
   pathdistance(b,c,20).
   pathdistance(c,d,5).
   pathdistance(d,e,15). 
   pathdistance(a,d,5).
   pathdistance(c,e,10).

solve(X,Z,C):-
     pathdistance(X,Z,C).

solve(X,Z,C):-
     pathdistance(X,Y,Cost),
     solve(Y,Z,C),
     Cost is Cost+C.

goal
    solve(a,d,Cost).

The answer I wanted for the Cost is the sum of all the C’s (total distance) between a and d.The above code is not working, it is not allowing me to take a new variable, Can please somebody do the changes in the above code so that i can get the total distance in Cost.Please keep in mind that I am new to prolog,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-05-27T06:16:43+00:00Added an answer on May 27, 2026 at 6:16 am

    You need to use an accumulator (another variable in your solve predicate) :

    pathdistance(a,b,10). 
    pathdistance(b,c,20).
    pathdistance(c,d,5).
    pathdistance(d,e,15). 
    pathdistance(a,d,5).
    pathdistance(c,e,10).
    
    solve(Start, End, Result):-
        solve(Start, End, 0, Result).
    

    Here you introduce your accumulator and initialize it to 0.

    solve(Start, End, TotalCost, Result) :-
        pathdistance(Start, End, Cost),
        Result is TotalCost + Cost.
    

    If this step is the final step, your result is the value of your accumulator (named TotalCost here) + the last cost.

    solve(Start, End, TotalCost, Result):-
        pathdistance(Start, Waypoint, Cost),
        NewTotalCost is TotalCost + Cost,
        solve(Waypoint, End, NewTotalCost, Result).
    

    If this is not the final step, you just increase your accumulator value by Cost.

    Hope this helps.

    You should have tagged this homework though since the problem has already received many downvotes due to poor question earlier in the day. Though this time you clearly show that you tried so it’s something else. Please ask if you need other informations.

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

Sidebar

Related Questions

domains list=symbol* predicates path(symbol,symbol) solve(symbol,symbol,list) insert(symbol,list,list) clauses path(a,b). path(b,c). path(c,d). path(d,e). path(a,d). path(c,e). solve(X,
What is the difference between Access and Filter predicates in Oracle execution plan? If
I trying to append where predicates and my goal is to create the same
I'm trying to write some predicates to solve the following task (learnprolognow.com) Suppose we
I have a large Prolog program with lots of predicates. I need to connect
I have always wondered why you cannot use locally defined classes as predicates to
How can I count nested list elements in Prolog? I have the following predicates
I understand that predicates are delegate to function which return bool and take generic
I'm building a series of predicates that duplicate lots of code, and so are
I am trying to learn how to use predicates and so am trying to

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.