简体   繁体   中英

How to check if each element in Prolog list is greater than 0?

I have the following Prolog predicate prototype: solution(+InputVector), where InputVector is a list of values of an unknown length. If all the values in the list are greater than 0, I print out a message. How do I do this?

if you are interested to extend your learning to 'higher order' predicates, consider maplist /2:

3 ?- maplist(<(0), [1,2,3]).
true.

4 ?- maplist(<(0),[0,1,2,3]).
false.

尝试检查列表是[]还是[X | Xs],并采取相应措施。

You were nearly there. Consider the following (updated thanks to @aBathologist):

  1|  solution([X]) :- 
  2|     X > 0, 
  3|     !,
  4|     write_ln('Success!').
  5|  solution([X|Y]) :- 
  6|     X > 0,
  7|     solution(Y).

Let's consider how this actually works, line-by-line:

  1. Defines the first clause of the predicate solution/1 which takes a single-element list containing X as an argument.
  2. Tests if item X is a number which is > 0 . If not, the predicate will terminate here and fail. Otherwise, Prolog will continue to the next line.
  3. While not strictly necessary, the cut ( ! ) here removes the choice-point Prolog would have generated at (1) given that the second clause on line (6) could also have been executed with the input [X] , as this is equivalent to [X|Y] where Y = [] . Therefore, this is a so-called 'grue'-cut for efficiency only.
  4. As the list [X] contains elements all greater than zero, the predicate prints a message and suceeds.
  5. Defines the second clause of the predicate solution/1 which takes a list containing one or more items, where X is the head of the list, and Y is the tail (remainder) of the list (which is itself a list which could be empty: [] ).
  6. Same as line (2).
  7. Proceed to recursively test the remainder of the list, Y .

Note that the above definition assumes that solution/1 must only succeed on non-empty lists of numbers greater than zero. If you wish to permit this predicate to succeed on empty lists, the implementation can be made even simpler:

solution([]) :- 
    write_ln('Success!').
solution([X|Y]) :-
    X > 0, 
    solution(Y).

In this version, either one of the two clauses of solution/1 is executed based on the argument: the first handles empty lists, and the second handles non-empty lists of numbers which are greater than zero. A cut ( ! ) is not necessary here as the predicate arguments are non-unifiable ( [] \\= [X|Y] ) and Prolog will not generate choice-points for any invocation of the first clause.

I hope this has been helpful, and has made some of the semantics of Prolog syntax clearer for you.

I'd write the predicate like this:

all_greater_than_zero([]).
all_greater_than_zero([H|T]) :-
    H > 0,
    all_greater_than_zero(T).

I assumed that an empty list was acceptable. If not, you can remove the first clause.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM