next up previous contents
Next: Return a Result Up: Program Patterns Previous: Test All Elements

Return a Result ---Having Processed One Element

Now we turn to the idea that we can return a result. This requires an extra argument to be carried around ---termed the result argument. We will now outline two further schemata that can be seen as developments of the two above. The first is intended to work through a list until an element satisfies some condition whereupon we stop and return some result. The schema is:



 return_after_event( Info
[HT]
Result):-

property( Info H)

result( Info H T Result).

return_after_event( Info [HeadTail] Ans):-

return_after_event( Info Tail Ans).

We will illustrate this with a predicate everything_after_a/2 that takes a list and returns that part of the list after any occurrence of the element a. We assume that the mode is mode everything_after_a(+ -).


everything_after_a([HeadTail]
Result):-

Head = a

Result = Tail.

everything_after_a([HeadTail] Ans):-

everything_after_a(Tail Ans).

Again there are no parameters. There is one input (also the recursion argument) and one output argument (also the result argument).

The first clause can be rewritten to:


everything_after_a([aTail]
Tail).

Again there is the same problem with this program as with the test for existence schema. The goal everything_after_a([d a s a f] X) will succeed with X=[s a f]. On redoing the goal can be resatisfied with X=[f]. This suggest that we have to be very careful about the meaning of this predicate.


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