Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

simple predicate

Status
Not open for further replies.

bill1001

Programmer
Joined
Aug 9, 2004
Messages
1
Location
IE
Im trying to figure out how write a predicate which replaces any occurence of a known color in a list with the word 'choice'. Known colors: red, green blue.
Eg:

?-color([the,hall,is,red,the,room,is,blue,and,green],R).

R=[the,hall,is,choice,the,room,is,choice,and,choice]
More(y/n)

Any help would be greatly appreciated.
Thank you.
 
Here's a start. Try to fill in the rest yourself.
Code:
is_color( red ).
is_color( green ).
is_color( blue ).

color( [], [] ).

% Replace the head of the list with 'choice'
% if it's a color
color( [ H | T1 ], [ choice | T2 ] :-
    ????,
    ????.

% Otherwise, leave that element alone
color( [ H | T1 ], [ H | T2 ] ) :-
    ????.

Hint: it's a recursive predicate.

Extra: How can you rewrite "is_color" so you can specify colors like this??
Code:
valid_colors( [ red, yellow, blue ] ).
valid_colors( [ orange, green, purple ] ).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top