Re: how to make this fail driven loop succesfull ?
Roelof Wobben <[email protected]>
| Newsgroups | gmane.comp.ai.prolog.swi |
|---|---|
| Message-ID | <[email protected]> |
---------------------------------------- > Date: Sun, 9 Mar 2014 09:46:23 +0100 > From: [email protected] > To: [email protected]; [email protected] > Subject: Re: [SWIPL] how to make this fail driven loop succesfull ? > > On 03/08/2014 10:43 PM, Bob Minors wrote: >> Roelof, >> >> You can make your failure-drive loop finish as 'true' like this: >> >> siblings(Father,Mother) :- is_father(X, Father), is_mother(X, Mother), write(X), nl, fail ; true. >> >> Reading from left-to-right, when it has found all the possible values >> for X and failed to find any more with backtracking, it will try the >> right-hand branch of the ';' and finally succeed with the 'true'. > > Over time, as proposed here before, I generally prefer recursion or > forall/2 and avoid side effects and lists of objects satisfying some > criterion as much as possible. So, you just write > > sibling(Father, Mother, X) :- > is_father(X, Father), > is_mother(X, Mother). > > You can easily combine this with other nice logical relations. If you want > all of them, you use findall/3, but as late as possible because you cannot > easily combine siblings(Father, Mother, Siblings) without using member, > set operations, etc, which all make your code less readable. If you merely > want to write them, use > > forall(sibling(Father, Mother, X), writeln(X)). > > These are a few lessons I learned while helping non-experienced programmers > using Prolog to explore their data. > > Cheers --- Jan > Thanks, As I stated Im following the adventure in Prolog tutorial. Im now at chapter 5 Rules where Recursion is chapter 8. So I cannot use recurrsion yet and have to use the fail-driven approach. Roelof