-2

Find the queries that will collect the right solutions given a database.

After that, we have to use these queries to create a predicate called "answer" such that typing "answer." in Prolog will write out all the solutions.

e.g. it's supposed to look like:

? - answer.
[solution to a]
[solution to b]
[solution to c]
true.

However, I'm not sure how to create a predicate where I don't have to pass any items/variables into it. Whenever I've tried to create a predicate "answer" and call it, I get errors saying "answer" cannot have 0 items.

edit: The question is:

Use your exact queries from above and create a predicate "answer" that prints using write the above answers, i.e., the following query is to work:

?- answer.

[solution to question a]

[solution to question b]

[solution to question c]

true.

false
  • 10,264
  • 13
  • 101
  • 209
  • What a title, if only there was one predicate that did that then we would need no more predicates. Can you please make your title more specific. – Guy Coder Mar 20 '19 at 13:34
  • 2
    Do us a favor an just post the assignment as given and what you have tried. Based on what you asked the only answer I know of at present is [42](https://en.wikipedia.org/wiki/42_(number)#The_Hitchhiker's_Guide_to_the_Galaxy). Even SWI-Prolog knows [this](https://stackoverflow.com/questions/45312575/who-gave-swi-prolog-a-sense-of-humor) – Guy Coder Mar 20 '19 at 13:41
  • I put the question as it's written in the assignment, but not sure if it makes it any clearer. – angryasheck Mar 20 '19 at 13:46
  • What are `your exact queries from above` please post them. – Guy Coder Mar 20 '19 at 13:54
  • So, if I get it right, the question is - how can a zero-argument predicate be used to show all solutions? – S.L. Barth is on codidact.com Mar 20 '19 at 13:58

1 Answers1

1

Facts to answer queries from above since none were given.

question_1(42).
question_2("Towel").
question_3("Thanks for all the fish.").

Code

answer :-
    question_1(Answer_1),
    format('Answer 1: ~w~n',[Answer_1]),
    question_2(Answer_2),
    format('Answer 2: ~w~n',[Answer_2]),
    question_3(Answer_3),
    format('Answer 3: ~w~n',[Answer_3]).

Example run:

?- answer.
Answer 1: 42
Answer 2: Towel
Answer 3: Thanks for all the fish.
true.

The only part of note is format/2 to print the answers.

Guy Coder
  • 24,501
  • 8
  • 71
  • 136