根据属性列表检查变量
checking a variable against a list of attributes
如何更改底部的谓词以递归解析它们的列表并与 X 进行比较。
/*knowledgeBase*/
lives_near_water(raccoon).
lives_near_water(muskrat).
eats_meat(raccoon).
eats_meat(wolverine).
eats_plants(raccoon).
eats_plants(cow).
has_bushy_tail(raccoon).
has_bushy_tail(squirrel).
has_bushy_tail(wolverine).
has_hair(muskrat).
has_hair(squirrel).
has_hair(rat).
has_hair(chipmunk).
has_hair(cow).
has_hair(wolverine).
has_long_incisors(muskrat).
has_long_incisors(rat).
striped(chipmunk).
can_fly(eagle).
has_hooked_beak(eagle).
has_feathers(eagle).
has_feathers(ostrich).
has_feathers(penguin).
can_swim(penguin).
produces_milk(cow).
gnaws_on_wood(squirrel).
/* Rules*/
mammal(X):- has_hair(X), produces_milk(X).
discontiguous has_long_incisors(X):- gnaws_on_wood(X).
discontiguous eats_plants(X):- gnaws_on_wood(X).
rodent(X):- has_long_incisors(X), mammal(X).
herbivore(X):- \+eats_meat(X), eats_plants(X).
carnivore(X):- eats_meat(X), \+eats_plants(X).
omnivore(X):- eats_meat(X), eats_plants(X).
muskrat(X):- rodent(X), lives_near_water(X), \+has_bushy_tail(X).
squirrel(X):- rodent(X), has_bushy_tail(X).
raccoon(X):- omnivore(X), lives_near_water(X), has_bushy_tail(X).
rat(x):- rodent(X), ^(has_bushy_tail(X)), \+striped(X).
chipmunk(X):- rodent(X), striped(X), \+has_bushy_tail(X).
wolverine(X):- mammal(X), carnivore(X), has_bushy_tail(X).
bird(X):- has_feathers(X).
eagle(X):- bird(X), carnivore(X), can_fly(X), has_hooked_beak(X).
ostrich(X):- bird(X), herbivore(X), \+can_fly(X), \+can_swim(X).
penguin(X):- bird(X), carnivore(X), \+can_fly(X), can_swim(X).
/*classify*/
animal_attribute([],X):- X == X.
animal_attribute([T1|Trest],X):- T1(X), animal_attribute(Trest,X).
not_animal_attribute([],X):- X == X.
not_animal_attribute([F1|Frest],X):- \+F1(X), not_animal_attribute(Frest,X).
classify(Ts,Fs,X):- animal_attribute(Ts,X), not_animal_attribute(Fs,X).
Classify 接受两个属性列表 Ts 和 Fs,Ts 是关于 X 应该为真的属性,Fs 是关于 X 不能为真的属性。
我的想法是让分类下面的两个谓词递归地检查 Ts/Fs 的每个成员与 X,我也在递归地尝试这个,因为我不知道他们会有多少成员而且我不允许更改分类变量的格式。
这是家庭作业,所以我不要求您进行更改,而是要求您进行递归的正确格式。
感谢您的帮助!
编辑:使分类部分递归,但我仍然收到语法错误:应为运算符
在两个递归函数之后。
在等待回复时回答了我自己的问题,如果有人遇到类似的问题虽然这里是解决我的问题的解决方案。
animal_attribute([],X):- X == X.
animal_attribute([T1|Trest],X):- call(T1,X), animal_attribute(Trest,X).
not_animal_attribute([],X):- X == X.
not_animal_attribute([F1|Frest],X):- \+call(F1,X), not_animal_attribute(Frest,X).
classify(Ts,Fs,X):- animal_attribute(Ts,X), not_animal_attribute(Fs,X).
您需要使用调用运算符来强制 prolog 将第一个变量视为谓词。
调用的格式是这样的:
呼叫(谓词,var1,...,varN)。
希望这对您有所帮助!
如何更改底部的谓词以递归解析它们的列表并与 X 进行比较。
/*knowledgeBase*/
lives_near_water(raccoon).
lives_near_water(muskrat).
eats_meat(raccoon).
eats_meat(wolverine).
eats_plants(raccoon).
eats_plants(cow).
has_bushy_tail(raccoon).
has_bushy_tail(squirrel).
has_bushy_tail(wolverine).
has_hair(muskrat).
has_hair(squirrel).
has_hair(rat).
has_hair(chipmunk).
has_hair(cow).
has_hair(wolverine).
has_long_incisors(muskrat).
has_long_incisors(rat).
striped(chipmunk).
can_fly(eagle).
has_hooked_beak(eagle).
has_feathers(eagle).
has_feathers(ostrich).
has_feathers(penguin).
can_swim(penguin).
produces_milk(cow).
gnaws_on_wood(squirrel).
/* Rules*/
mammal(X):- has_hair(X), produces_milk(X).
discontiguous has_long_incisors(X):- gnaws_on_wood(X).
discontiguous eats_plants(X):- gnaws_on_wood(X).
rodent(X):- has_long_incisors(X), mammal(X).
herbivore(X):- \+eats_meat(X), eats_plants(X).
carnivore(X):- eats_meat(X), \+eats_plants(X).
omnivore(X):- eats_meat(X), eats_plants(X).
muskrat(X):- rodent(X), lives_near_water(X), \+has_bushy_tail(X).
squirrel(X):- rodent(X), has_bushy_tail(X).
raccoon(X):- omnivore(X), lives_near_water(X), has_bushy_tail(X).
rat(x):- rodent(X), ^(has_bushy_tail(X)), \+striped(X).
chipmunk(X):- rodent(X), striped(X), \+has_bushy_tail(X).
wolverine(X):- mammal(X), carnivore(X), has_bushy_tail(X).
bird(X):- has_feathers(X).
eagle(X):- bird(X), carnivore(X), can_fly(X), has_hooked_beak(X).
ostrich(X):- bird(X), herbivore(X), \+can_fly(X), \+can_swim(X).
penguin(X):- bird(X), carnivore(X), \+can_fly(X), can_swim(X).
/*classify*/
animal_attribute([],X):- X == X.
animal_attribute([T1|Trest],X):- T1(X), animal_attribute(Trest,X).
not_animal_attribute([],X):- X == X.
not_animal_attribute([F1|Frest],X):- \+F1(X), not_animal_attribute(Frest,X).
classify(Ts,Fs,X):- animal_attribute(Ts,X), not_animal_attribute(Fs,X).
Classify 接受两个属性列表 Ts 和 Fs,Ts 是关于 X 应该为真的属性,Fs 是关于 X 不能为真的属性。
我的想法是让分类下面的两个谓词递归地检查 Ts/Fs 的每个成员与 X,我也在递归地尝试这个,因为我不知道他们会有多少成员而且我不允许更改分类变量的格式。
这是家庭作业,所以我不要求您进行更改,而是要求您进行递归的正确格式。
感谢您的帮助!
编辑:使分类部分递归,但我仍然收到语法错误:应为运算符 在两个递归函数之后。
在等待回复时回答了我自己的问题,如果有人遇到类似的问题虽然这里是解决我的问题的解决方案。
animal_attribute([],X):- X == X.
animal_attribute([T1|Trest],X):- call(T1,X), animal_attribute(Trest,X).
not_animal_attribute([],X):- X == X.
not_animal_attribute([F1|Frest],X):- \+call(F1,X), not_animal_attribute(Frest,X).
classify(Ts,Fs,X):- animal_attribute(Ts,X), not_animal_attribute(Fs,X).
您需要使用调用运算符来强制 prolog 将第一个变量视为谓词。 调用的格式是这样的: 呼叫(谓词,var1,...,varN)。 希望这对您有所帮助!