Using cuts in Prolog -
i trying find of distinct entries person's name john, peter or fred.
however, if there were, example, 2 people called peter, want display 1 occurrence of name.
my code far follows:
searchpeople(x) :- people(_,[x|_]), x=john; x=peter; x=fred.
i understand solution cuts (having read other posts), cannot find example cuts used when trying retrieve x or y or z (in case john, peter or fred).
thanks in advance.
the problem you're confusing operator precedence. more conventional programming languages writing this
if ( , b or c or d ) ...
is going in trouble, code has exact same problem. operator precedence , associativity causes
searchpeople(x) :- people(_,[x|_]) , x=john ; x=peter ; x=fred .
to parsed if written
searchpeople(x) :- ( people(_,[x|_]) , x = john ) ; ( x = peter ; x = fred ) .
which not intended.
while could use parenthesis effect want:
searchpeople(x) :- people(_,[x|_]) , ( x = john ; x = peter ; x = fred ) .
you better off splitting things bit:
search_people(x) :- people(_,[x|_]) , desired_person(x). desired_person(john). desired_person(peter). desired_person(fred).
it makes intent clearer , easier understand. it's easier debug , extend.
Comments
Post a Comment