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

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

问题描述

今天早些时候,我寻求帮助以建立序言中的数据库以及如何按参数搜索,有人想出了这一点:

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

它工作得很好,但是命令"member"似乎不符合规则:

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.

如果我这样查询:

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

它将开始列出数据库中的每个组件,并且似乎发生了这种情况,因为没有为变量分配任何内容. 整个数据库在这里: https://pastebin.com/1Yy6cTV9

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

这将产生3向联接.而且,您可以使用更多变量从获得的三个记录中获取更多信息:

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天全站免登陆