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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T02:37:28+00:00 2026-06-05T02:37:28+00:00

For an assignment we are asked to write a predicate route/3 that succeeds if

  • 0

For an assignment we are asked to write a predicate route/3 that succeeds if there is a route from Start to Finish, visiting town in the list Visits.

I’ve come up with an idea for a solution but for some reason the Visits list is always empty after route terminates and I can’t figure out why that is. I’m not looking for a solution to the question, just something to point me in the right direction.

Here is my code so far:

Note that my calls to write are simply for debugging reasons.

road('Wellington', 'Palmerston North', 143).
road('Palmerston North', 'Wanganui', 74).
road('Palmerston North', 'Napier', 178).
road('Palmerston North', 'Taupo', 259).
road('Wanganui', 'Taupo', 231).
road('Wanganui', 'New Plymouth', 163).
road('Wanganui', 'Napier', 252).
road('Napier', 'Taupo', 147).
road('Napier', 'Gisborne', 215).
road('New Plymouth', 'Hamilton', 242).
road('New Plymouth', 'Taupo', 289).
road('Taupo', 'Hamilton', 153).
road('Taupo', 'Rotorua', 82).
road('Taupo', 'Gisborne', 334).
road('Gisborne', 'Rotorua', 291).
road('Rotorua', 'Hamilton', 109).
road('Hamilton', 'Auckland', 126).

route(Start, Start, Visits) :-
    write(Visits), nl.

route(Start, Finish, Visits) :-
    write(Visits),nl,
    road(Finish, From, _),
    route(Start, From, [From | Visits]).

test :-
    Visits = [],
    route('Auckland', 'Wellington', Visits),
    write(Visits).

The output when running test.:

1 ?- test.
[]
[Palmerston North]
[Wanganui,Palmerston North]
[Taupo,Wanganui,Palmerston North]
[Hamilton,Taupo,Wanganui,Palmerston North]
[Auckland,Hamilton,Taupo,Wanganui,Palmerston North]
[]
true .
  • 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-05T02:37:29+00:00Added an answer on June 5, 2026 at 2:37 am

    Variables in Prolog are unified, not assigned. Surely this argument is part of your course.

    Then Visits in test keeps the empty list (the accumulator) you ‘assigned’ when start. You should add (or better, insert) an argument to route, that holds the built list. Something like

    test :-
        route('Auckland', 'Wellington', [], Visits),
        write(Visits).
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been asked to write a program using python for an assignment. I
During an assignment, I was asked to show that a hash table of size
For a mandatory assignment of a DB2 class I'm asked to write o procedure
I have an assignment that has asked me to copy a file using buffered
I had a homework assignment that asked for a function that uses direct recursion
My assignment is to write an overloaded version of iquote() , a method that
For an assignment we were asked to create a function that would reverse all
For an assignment we were asked to define a fibonacci function, which I accomplished
I have a programming assignment and we are asked to create a program in
I'm doing an assignment for my Data Structures class. we were asked to 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.