如何返回Prolog中的建议列表



对于我的作业,我应该列出20个潜在的宠物,然后定义有关每个宠物的事实。然后,我需要问潜在的宠物主五个问题,这将有助于确定哪些宠物是一个很好的建议。我正在尝试根据用户输入返回宠物列表,但每次都返回true,实际上并未列出推荐的宠物。不确定我出错了哪里。我只会在我的代码示例中包含一些宠物,所以它不会太长。

pet_advisor.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]的列表,但它返回的所有内容都是正确的。

您的代码有几个问题。首先,在大多数序言系统和序言标准中,必须声明不连续的谓词。在文件的开头中添加以下指令:

:- discontiguous([
    size/2, sleeps/2, stays/2, class/2, live/2
]).

接下来,不需要使用动态谓词并断言和缩回事实,您可以查询建议:

which_pet(Size, Sleep, Place, Type, Age, Pet) :-
    size(Pet, Size),
    sleeps(Pet, Sleep),
    stays(Pet, Place),
    class(Pet, Type),
    live(Pet, Age0), Age0 =< Age.
recommend :-
    write('Do you want a small, medium, or large sized pet? '), read(Size), nl,
    write('Do you want a pet that sleeps during the day or night? '), read(Sleep),
    write('Do you want an indoor or outdoor pet? '), read(Place), nl,
    write('Do you want a reptile, mammal, bird, or a fish? '), read(Type), nl,
    write('How long do you want your pet to live (years)? '), read(Age), nl,
    findall(Pet, which_pet(Size,Sleep,Place,Type,Age,Pet), Suggestions),
    write('I would recommend these pets for you: '), nl, writelist(Suggestions).

这不是理想的重写,因为它比使用动态谓词好得多,但要好得多。

作为结局,您打印结果的代码执行了两个可以更好分开的任务:(1)过滤重复项和(2)打印唯一的结果。我建议您将这些任务分开。过滤结果可以完成,例如通过使用setof/2代替findall/3或通过findall/3调用构建的列表上调用sort/2。我把那个改写给你。还使用标准的否定控制结构+/1,而不是遗留/弃用的not/1谓词。

示例调用:

| ?- recommend.
Do you want a small, medium, or large sized pet? small.
Do you want a pet that sleeps during the day or night? night.
Do you want an indoor or outdoor pet? outdoor.
Do you want a reptile, mammal, bird, or a fish? bird.
How long do you want your pet to live (years)? 20.
I would recommend these pets for you: 
chicken
yes

相关内容

  • 没有找到相关文章

最新更新