如何在 Prolog 中为变量(如字符串)分配多个值? [英] How can I assign multiple values to a variable (like a string) in Prolog?

查看:28
本文介绍了如何在 Prolog 中为变量(如字符串)分配多个值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天早些时候我请求帮助在 prolog 中构建数据库以及如何通过参数搜索,有人想出了这个:

<块引用>

您还可以为每个处理器添加术语列表,例如:

processor(pentium_g4400, [brand('intel'), family('pentium'),系列('g4400'),时钟(3.3),套接字('lga1151'), ram('ddr4'),核心(2),线程(2)]).

<块引用>

在这种情况下,您可以查询:

processor(Proc, Specs), member(family('pentium'), Specs).

它运行得很好,但是,命令member"似乎不适用于规则:

build(ProcSpecs, RamSpecs, MoboSpecs):-处理器(NameProc,PSpec),成员(ProcSpecs,PSpec),component_ram(NameRam, RSpec), member(RamSpecs, RSpec),主板(NameMobo,MSpec),成员(MoboSpecs,MSpec),写(-----------------------------"),NL,写(处理器:"),写(NomeProc),NL,写(Ram:"),写(NomeRam),NL,写(主板:"),写(NomeMobo),nl.

如果我这样查询:

build(ram(ddr4), ram(ddr4), ram(ddr4)).

(或者输入我想搜索的任何其他参数)它回答我一个与参数匹配的组件列表(这是我正在寻找的结果).但是,如果我尝试使用成员"来设置多个参数,如下所示:

build(ProcSpecs, RamSpecs, MoboSpecs), member(socket('lga1151'), ProcSpecs), member(ram('ddr4'), ProcSpecs), member(ram('ddr41'), RamSpecs),成员(套接字('lga1151'),MoboSpecs),成员(ram('ddr4'),MoboSpecs).

它开始列出数据库中的每个组件,看起来它发生是因为没有为变量分配任何内容.整个数据库在这里:https://pastebin.com/1Yy6cTV9

解决方案

看看你的数据库文件,我认为你今天早些时候得到的建议不是正确的方向.我可能会把它当作一个直接的关系数据库来处理,有一个这样的表:

% 处理器(Id、品牌、系列、系列、时钟、插槽、RAM、内核、线程).

然后我会像这样重铸你制作的数据库:

处理器(奔腾_g4400、英特尔、奔腾、g4400、3.3、lga1151、ddr4、2、2).

这就是您对关系数据库所做的事情,我认为这也是您在这里应该做的事情.现在,如果您只想查询一个方面,则可以按位置进行:

?- 处理器(处理器,_,奔腾,_,_,_,_,_,_).处理器 = 奔腾_g4400 ;...

然后你会对另外两个关系做同样的事情:

component_ram(corsair_dominator_platinum_8gb_ddr4, corsair, dominator, Platinum, '8gb', ddr4).主板(asus_prime_z270m-plus、asus、prime_z270m-plus、z270、lga1151、4、ddr4).

现在进行连接"只需在两个地方使用相同的变量即可:

?- 处理器(处理器, _, _, _, _, Socket_type, RAM_type, _, _),component_ram(RAM, _, _, _, _, RAM_type),主板(主板,_,_,_,Socket_type,_,RAM_type).

这会产生 3 路连接.并且你可以使用更多的变量从你获取的三个记录中获取更多的信息:

处理器 = pentium_g4400,Socket_type = lga1151,RAM_type = DDR4,RAM = corsair_dominator_platinum_8gb_ddr4,主板 = asus_prime_z270m-plus.

Earlier today I asked for help for building a database in prolog and how to search by parameters, and somebody came up with this:

You can also add a list of terms to every processor, like:

processor(pentium_g4400, [brand('intel'),    family('pentium'),
                          series('g4400'),   clock(3.3),
                          socket('lga1151'), ram('ddr4'),
                          cores(2),          threads(2)]).

In that case you can query with:

processor(Proc, Specs), member(family('pentium'), Specs).

And it worked pretty well, but, the command "member" does not seem to work with a rule:

build(ProcSpecs, RamSpecs, MoboSpecs):-
        processor(NameProc, PSpec), member(ProcSpecs, PSpec),
        component_ram(NameRam, RSpec), member(RamSpecs, RSpec),
        motherboard(NameMobo, MSpec), member(MoboSpecs, MSpec),
        write("-----------------------------"), nl, 
        write("Processor: "), write(NomeProc), nl,
        write("Ram: "), write(NomeRam), nl,
        write("Motherboard: "), write(NomeMobo), nl.

If I query like this:

build(ram(ddr4), ram(ddr4), ram(ddr4)).

(Or putting any other parameters I would like to search) It answer me a list of components that match the parameter (which is the result I'm looking for). But, if I try to use "member" to set more than one parameter, like this:

build(ProcSpecs, RamSpecs, MoboSpecs), member(socket('lga1151'), ProcSpecs), member(ram('ddr4'), ProcSpecs), member(ram('ddr41'), RamSpecs), member(socket('lga1151'), MoboSpecs), member(ram('ddr4'), MoboSpecs).

It starts listing every component on the database, and it seems like it happens because nothing is being assigned to the variable. The whole database is here: https://pastebin.com/1Yy6cTV9

解决方案

Looking at your database file, I think the advice you got earlier today wasn't the right direction. I would probably handle this as a straight relational database, with a table like this:

% processor(Id, Brand, Family, Series, Clock, Socket, RAM, Cores, Threads).

Then I would recast the DB you made like so:

processor(pentium_g4400, intel, pentium, g4400, 3.3, lga1151, ddr4, 2, 2).

This is what you'd do with a relational database and I think it's what you should do here as well. Now if you want to query on just one aspect, you do it positionally:

?- processor(Processor, _, pentium, _, _, _, _, _, _).
Processor = pentium_g4400 ;
...

Then you'd do the same thing with your two other relations:

component_ram(corsair_dominator_platinum_8gb_ddr4, corsair, dominator, platinum, '8gb', ddr4).
motherboard(asus_prime_z270m-plus, asus, prime_z270m-plus, z270, lga1151, 4, ddr4).

Now doing a "join" is done simply by using the same variable in two places:

?- processor(Processor, _, _, _, _, Socket_type, RAM_type, _, _),
   component_ram(RAM, _, _, _, _, RAM_type),
   motherboard(Motherboard, _, _, _, Socket_type, _, RAM_type).

This produces the 3-way join. And you can use more variables to obtain more information from the three records you obtain:

Processor = pentium_g4400,
Socket_type = lga1151,
RAM_type = ddr4,
RAM = corsair_dominator_platinum_8gb_ddr4,
Motherboard = asus_prime_z270m-plus.

这篇关于如何在 Prolog 中为变量(如字符串)分配多个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆