对于我的任务,我应该列出 20 只潜在宠物,然后定义关于每只宠物的事实。然后我需要问潜在的宠物主人五个问题,这将有助于决定哪些宠物是好的推荐。我正在尝试根据用户输入返回宠物列表,但它每次都返回 true,实际上并没有列出推荐的宠物。不知道我哪里出错了。我只会在我的代码示例中包含一些宠物,所以它不会太长。
宠物顾问.pl:
pet(cat).
pet(chameleon).
pet(chicken).
pet(chinchilla).
pet(cow).
size(cat, small).
sleeps(cat, night).
stays(cat, indoor).
stays(cat, outdoor).
class(cat, mammal).
live(cat, 12)
size(chameleon, small).
sleeps(chameleon, night).
stays(chameleon, indoor).
class(chameleon, reptile).
live(chameleon,5).
size(chicken, small).
sleeps(chicken, night).
stays(chicken, outdoor).
class(chicken, bird).
live(chicken,10).
size(chinchilla, small).
sleeps(chinchilla, day).
stays(chinchilla, indoor).
class(chinchilla, mammal).
live(chinchilla,15).
size(cow, large).
sleeps(cow, night).
stays(cow, outdoor).
class(cow, mammal).
live(cow,22).
pet_size_ok(X) :- pet_size(X), size(Y, X).
sleep_type_ok(X) :- sleep_type(X), sleeps(Y, X).
pet_location_ok(X) :- pet_location(X), stays(Y, X).
kind_ok(X) :- kind(X), class(Y, X).
life_ok(X) :- life(X), live(Y, Z), Z =< X.
which_pet(X) :- pet_size_ok(X), sleep_type_ok(X), pet_location_ok(X), kind_ok(X), life_ok(X).
recommend :- write('Do you want a small, medium, or large sized pet? '), read(Size), nl, assert(pet_size(Size)),
write('Do you want a pet that sleeps during the day or night? '), read(Sleep), nl, assert(sleep_type(Sleep)),
write('Do you want an indoor or outdoor pet? '), read(Place), nl, assert(pet_location(Place)),
write('Do you want a reptile, mammal, bird, or a fish? '), read(Type), nl, assert(kind(Type)),
write('How long do you want your pet to live (years)? '), read(Age), nl, assert(life(Age)),
findall(Pets, which_pet(Pets), Suggestions),
write('I would recommend these pets for you: '), nl, writelist(Suggestions),
retract(pet_size(Size)), retract(sleep_type(Sleep)),
retract(pet_location(Place)),
retract(kind(Type)), retract(life(Age)).
writelist([]).
writelist([H|T]) :- writeonce(H,T), writelist(T).
writeonce(H,T) :- member(H,T).
writeonce(H,T) :- not(member(H,T)), write(H), nl.
所以如果我要回答这样的问题:小型夜间室内哺乳动物 15
它应该返回一个带有 [cat, chinchilla] 的列表,但它返回的都是真的。