I need to find the first element on the list which satisfies a user provided goal. I mean something like maplist/2
but which succeeds when the goal can be applied to at least one element. include/3
would be an option but I'm after a more optimal solution that can stop after finding the first element.
I think the interface could look like:
first(:Goal, List, First)
and it should be semidet in the SWI-Prolog sense.
It's fairly easy to code it but I'd prefer an existing rule. Is there a relevant rule in the "standard" libraries; I'm using SWI-Prolog.
Cheers, Jacek
include_item(:Goal, List, Item)
which succeeds for eachItem
inList
that allows:Goal
to succeed, then useonce/1
on the call toinclude_item/3
. In effect, a version ofinclude/3
could be written asinclude(Goal, List, Items) :- findall(X, include_item(Goal, List, X), Items).
. – lurker