English 中文(简体)
用 pro语印刷所有记录
原标题:print all records using query in prolog
  • 时间:2012-01-12 03:47:33
  •  标签:
  • prolog
lectures(monday, nolecture).
lectures(tuesday, vp).
lectures(tuesday, se).
lectures(tuesday, ddbms).
lectures(wednessday, ds).
lectures(wednessday, mpl).
lectures(thursday, vp).
lectures(thrusday, se).
lectures(friday, ds).
lectures(friday, mpl).
lectures(saturday, ai).
lectures(saturday, ddbms).

?- lectures(friday, X), write(X),nl.

问询仅作为ds的第一记录印发,我希望在特定日即星期五印发所有记录,结果应为dsmpl

各位

最佳回答

在问询结束时添加<代码>fail.:

| ?- lectures(friday, X), write(X), nl, fail.
ds
mpl

no
| ?- 

另见。 这一非常相似的问题试图在提出新问题之前再用一点点。

问题回答

b 过去,如果你想让它更加可行,你可以总结如下:

display_lectures(Day) :-
    lectures(Day, X),
    write(X),
    nl,
    fail
    ;
    true.

页: 1 这里的一条是真正的禁令。 这使这一呼吁能够在更大程度上得到响应,因此我认为我已提到这一点。

And alternatively, here is another way to do it (if your implementation has some sort of forall/2) :

display_lectures(Day) :- forall(lectures(Day, X), (write(X), nl)).




相关问题
Prolog : Learning by example

I am trying to learn a little bit about swi-prolog (beyond the basic, useless programs). Can anyone explain (perhaps in pseudocode) what this sudoku solver and the related functions are doing? If ...

Working with lists in Prolog

First off let me state that this is part of a class exercise given as homework. But, the entire assignment is much more involved than the subject of this question. So.. I am searching through two ...

SWI-Prolog conditional statements

I m trying to write a function that will test to see if the word hello is contained in a list. If it is contained, i don t want it to say "true", i want it to say : "yes, the word hello is contained ...

prolog cut off in method

I have a question I would like to ask you something about a code snippet: insert_pq(State, [], [State]) :- !. insert_pq(State, [H|Tail], [State, H|Tail]) :- precedes(State, H). insert_pq(State, [...

Max out of values defined by prolog clauses

I know how to iterate over lists in Prolog to find the maximum, but what if each thing is a separate clause? For example if I had a bunch of felines and their ages, how would I find the oldest kitty? ...

热门标签