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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:24:06+00:00 2026-05-17T21:24:06+00:00

TL;DR: Need help calling a rule with a fact I´ve started out with Prolog,

  • 0

TL;DR: Need help calling a rule with a fact
I´ve started out with Prolog, coming from C and got stuff working… until they evidently got broken. I´m writing a small car-paint program for myself as I´m learning this new language

I’m trying to call a rule with a fact (is this possible?), what I want to do is use one fact “cars” and another fact “paint” to make one big list consisting of all the cars in all the different paints. I’m having trouble making the code work as I want…have a look

I´ve got the facts:

cars([ferrari, bmw, ford, jaguar]).  
paints([red, white, blue, yellow]).  

/*Now I wanted to loop through each car, eachtime printing 
out the different paint combinations of that car:  */

start:- loop_cars(cars(C)).  /*starts loop_cars with all the cars e.g [ferrari...]*/
                             /*but it false here, even if C = [ferrari...]*/
loop_cars([]).  
loop_cars([Ca|Rest]):-  
    loop_paints(Ca,paints(P)),  /*The car is sent off for a paint job,...*/
    loop_cars(Rest).            /*...(cont from above) same false here as before*/

loop_paints(_,[]).  
loop_paints(Ca,[Pa|Rest]):-  /*This works*/
    write([Ca,Pa]),  /*Writes it like "[ferrari, white] [ferrari, blue] ..."*/
    loop_paints(Ca,Rest).  

So I guess I need help solving two problems:

  • How do i pass the contents of the facts cars and paints to the two loops?
  • A “garage” to put all the combinations in. Garage being a big list consisting of small 2-items-lists (the car and paint).
  • 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-17T21:24:07+00:00Added an answer on May 17, 2026 at 9:24 pm

    You can do it like this:

    start :- cars(C), loop_cars(C).
    

    First, “assign” (I think it’s called “unify” in Prolog terminology) the list of cars to the variable C and then call loop_cars for this list. Similarly with paints.

    If you want to store the result in a variable, you have to add an “output” parametr to your predicates:

    loop_paints(_,[],[]).  
    loop_paints(Ca,[Pa|Rest],[Res|ResRest]):-
        Res = [Ca,Pa],
        loop_paints(Ca,Rest,ResRest).
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.