next up previous contents
Next: Return a Result Up: Program Patterns Previous: Test for Existence

Test All Elements

In this situation we require that the elements of a list all satisfy some property. Here is the general schema:

 test_all_have_property( Info,[]).

test_all_have_property( Info,[HeadTail]):-

element_has_property( Info,Head),

test_all_have_property( Info,Tail).

Again, the expression Info stands for a specific number of parameters that carry information needed for the determination that an individual element has the desired property. The remaining argument is the recursion argument. We illustrate with a predicate digits/1 for testing that a list of elements consists of digits only. We assume that we have mode all_digits(+).




plus definition of member/2.

This predicate has a declarative reading that a list has the property of consisting of digits if the first element is a digit and the tail of the list has the property of consisting of digits.
Note that we can make this fit the schema better if the term [0,1,2,3,4,5,6,7,8,9] is passed in as a parameter.

Paul Brna
Mon May 24 20:14:48 BST 1999