List 如何在Prolog中获取对象列表
我正在解决一些序言练习,这时我发现自己在解决以下问题时遇到了一些困难: 假设你有关于对象的事实基础:List 如何在Prolog中获取对象列表,list,prolog,prolog-findall,List,Prolog,Prolog Findall,我正在解决一些序言练习,这时我发现自己在解决以下问题时遇到了一些困难: 假设你有关于对象的事实基础: object(obj1). object(obj2). object(obj3). object(obj4). object(obj5). material(obj1,wood). material(obj2,wood). material(obj3, glass). material(obj4, glass). material(obj5, iron). type(obj1
object(obj1).
object(obj2).
object(obj3).
object(obj4).
object(obj5).
material(obj1,wood).
material(obj2,wood).
material(obj3, glass).
material(obj4, glass).
material(obj5, iron).
type(obj1, able).
type(obj2, chair).
type(obj3, mesa).
type(obj4, jar).
type(obj5, rattle).
weight(obj1, 10.5).
weight(obj2, 1.5).
weight(obj3, 1.6).
weight(obj4, 0.5).
weight(obj5, 1.8).
现在的想法是制作谓词对象描述(List),其中List是每个对象与其特征的连接,类似于:
([obj1-wood-table-10.5, obj2-wood-chair-1.5, …, obj5-iron-rattle-1.8] )
我尝试使用bagof和findall,但找不到正确的答案
Thx预先我已经更改了输入格式。现在搜索起来容易一点。 我希望这样可以
obj(obj1, material, wood).
obj(obj2, material, wood).
obj(obj3, material, glass).
obj(obj4, material, glass).
obj(obj5, material, iron).
obj(obj1, type, table).
obj(obj2, type, chair).
obj(obj3, type, mesa).
obj(obj4, type, jar).
obj(obj5, type, rattle).
obj(obj1, weight, 10.5).
obj(obj2, weight, 1.5).
obj(obj3, weight, 1.6).
obj(obj4, weight, 0.5).
obj(obj5, weight, 1.8).
给定此输入格式,您现在可以将其映射到(列表)列表,例如:
object_description(List) :-
findall(Id-TmpList, bagof(Type-Value, obj(Id, Type, Value), TmpList), List).
这不会产生您在问题中所遇到的确切输出格式,但会提供类似的输出格式(并且可能更易于进一步处理)
用法:
?- object_description(List).
List = [obj1-[material-wood, type-table, weight-10.5],
obj2-[material-wood, type-chair, weight-1.5],
obj3-[material-glass, type-mesa, weight-1.6],
obj4-[material-glass, type-jar, weight-0.5],
obj5-[material-iron, type-rattle, ... - ...]].
我已经更改了输入格式。现在搜索起来容易一点。 我希望这样可以
obj(obj1, material, wood).
obj(obj2, material, wood).
obj(obj3, material, glass).
obj(obj4, material, glass).
obj(obj5, material, iron).
obj(obj1, type, table).
obj(obj2, type, chair).
obj(obj3, type, mesa).
obj(obj4, type, jar).
obj(obj5, type, rattle).
obj(obj1, weight, 10.5).
obj(obj2, weight, 1.5).
obj(obj3, weight, 1.6).
obj(obj4, weight, 0.5).
obj(obj5, weight, 1.8).
给定此输入格式,您现在可以将其映射到(列表)列表,例如:
object_description(List) :-
findall(Id-TmpList, bagof(Type-Value, obj(Id, Type, Value), TmpList), List).
这不会产生您在问题中所遇到的确切输出格式,但会提供类似的输出格式(并且可能更易于进一步处理)
用法:
?- object_description(List).
List = [obj1-[material-wood, type-table, weight-10.5],
obj2-[material-wood, type-chair, weight-1.5],
obj3-[material-glass, type-mesa, weight-1.6],
obj4-[material-glass, type-jar, weight-0.5],
obj5-[material-iron, type-rattle, ... - ...]].