如何在Prolog中为变量(如字符串)分配多个值?

编程入门 行业动态 更新时间:2024-10-22 13:29:36
本文介绍了如何在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).

它将开始列出数据库中的每个组件,并且似乎发生了这种情况,因为没有为变量分配任何内容. 整个数据库在这里: pastebin/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: pastebin/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中为变量(如字符串)分配多个值?

本文发布于:2023-10-27 03:20:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1532163.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   字符串   变量   中为   分配

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!