如何获取 Prolog 中的对象列表

发布于 2024-08-17 19:29:24 字数 694 浏览 13 评论 0原文

当我在解决一些序言练习时,我发现自己在解决以下问题时遇到了一些困难: 考虑你有关于对象的事实基础:

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).  

现在的想法是创建谓词 object_description(List),其中 List 是每个对象与其特征的连接,类似于:

([obj1-wood-table-10.5, obj2-wood-chair-1.5, …, obj5-iron-rattle-1.8] ) 

我尝试使用 bagof 和 findall 但找不到正确的答案。

提前谢谢

I was resolving some prolog exercises when I fond myself with some difficulties resolving the following one:
Consider you have this fact base about object:

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).  

Now the idea is to make the predicate object_description(List) where List is the joining of each object with it's caracteristics, something like:

([obj1-wood-table-10.5, obj2-wood-chair-1.5, …, obj5-iron-rattle-1.8] ) 

I tried using bagof and findall but couldn't find the right answer.

Thx in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

抱猫软卧 2024-08-24 19:29:24
 ?- findall(O-M-T-W,(object(O),material(O,M),type(O,T),weight(O,W)),Res).
Res = [obj1-wood-able-10.5, obj2-wood-chair-1.5, obj3-glass-mesa-1.6, obj4-glass-jar-0.5, obj5-iron-rattle-1.8].
 ?- findall(O-M-T-W,(object(O),material(O,M),type(O,T),weight(O,W)),Res).
Res = [obj1-wood-able-10.5, obj2-wood-chair-1.5, obj3-glass-mesa-1.6, obj4-glass-jar-0.5, obj5-iron-rattle-1.8].
初见你 2024-08-24 19:29:24
classic style of prolog :

    member(_, []):-!,fail.
    member(X, [X| _]).
    member(X, [_|T]):- member(X,T).

    object_description(R):-
           get_all_objects([], R).

    get_all_objects(T, [H|R]):-
          get_object(H),
          not(member(H,T)),
          get_all_objects([H|T], R).
    get_all_objects([], []).

    get_object(Obj):-
          object(X),
          material(X,M),
          type(X, T),
          weight(X, W),
          concat(X, "-", R1),
          concat(R1, M, R2),
          concat(R2, "-", R3),
          concat(R3, T, R4),
          concat(R4, "-", R5),
          concat(R5, W, Obj).

% // concat(str1, str2, str3) if your compilator have'nt you must make it or use other %//analog, idea is  str1+str2=str3
classic style of prolog :

    member(_, []):-!,fail.
    member(X, [X| _]).
    member(X, [_|T]):- member(X,T).

    object_description(R):-
           get_all_objects([], R).

    get_all_objects(T, [H|R]):-
          get_object(H),
          not(member(H,T)),
          get_all_objects([H|T], R).
    get_all_objects([], []).

    get_object(Obj):-
          object(X),
          material(X,M),
          type(X, T),
          weight(X, W),
          concat(X, "-", R1),
          concat(R1, M, R2),
          concat(R2, "-", R3),
          concat(R3, T, R4),
          concat(R4, "-", R5),
          concat(R5, W, Obj).

% // concat(str1, str2, str3) if your compilator have'nt you must make it or use other %//analog, idea is  str1+str2=str3
口干舌燥 2024-08-24 19:29:24

我改变了输入格式。现在搜索起来容易一些。
我希望这样就可以了。

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, ... - ...]].

I've changed the input format. It's a little easier to search now.
I hope it's OK like this.

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).

Given this input format you can now map it to a list (of lists) e.g. like this:

object_description(List) :-
    findall(Id-TmpList, bagof(Type-Value, obj(Id, Type, Value), TmpList), List).

This doesn't produce the exact output format that you have in the question, but does give something similar (and maybe easier to process further).

Usage:

?- 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, ... - ...]].
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文