整数与其名称之间的关系 - Prolog [英] Relationship between integers and their names - Prolog

查看:61
本文介绍了整数与其名称之间的关系 - Prolog的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Prolog 中探索自然数、皮亚诺数、算术等的概念.现在我正在尝试创建一个谓词,它允许我输入任何数字的名称并获取它的数值(反之亦然).我该怎么办?我的想法是我会翻译给定的数字,然后使用加号函数将它们加在一起(例如一百四十五:一,百 = 100,而 = 0,四十 = 40,五 = 5 -> 100 + 0+ 40 + 5 = 145.

I'm trying to explore the concepts of natural numbers, Peano numbers, arithmetic, etc. in Prolog. Right now I'm trying to create a predicate that will allow me to enter the name of any number and get it's numerical value (or vice versa). How would I go about this? My idea was that I would translate the numbers given and them add them together using the plus function (e.g. one hundred and forty-five: one, hundred = 100, and = 0, forty = 40, five = 5 -> 100 + 0 + 40 + 5 = 145.

以下是一些查询的示例:

Here's an example of some queries:

?- numnames(X, 375).
X = [three, hundred, and, seventy, five] ;
false.
?- numnames([seven], 7).
true ;
false.

以下是我的一些详尽事实(我只是挑选了一些可以解决某个类别的事实):

Here are some of my exhaustive facts (I just picked some that would address a certain category):

numnames([],0).
numnames(and,0).
numnames([one],1).
numnames([ninety],90).
numnames([one, hundred], 100).

我只是不知道如何在算术之前翻译数字,以及我在哪里/何时停止制作详尽的事实并开始制定规则?感谢您的帮助.

I'm just confused as to how to translate the numbers before the arithmetic, and also where/when do I stop making exhaustive facts and start making rules? Thanks for the help.

推荐答案

这是一个很好的 Prolog 语法规则DCGs 应用程序(基本上是隐藏一些列表的语法糖操作并且可以相对直接地转换为普通 Prolog 规则).

This is a nice application for Prolog grammar rules or DCGs (basically syntactic sugar that hides some list manipulation and has a relatively straightforward translation to normal Prolog rules).

num_0_999(0) --> [zero].
num_0_999(N) --> num_1_99(N).
num_0_999(N) --> num_100_999(N).

num_1_99(N) --> num_1_9(N).
num_1_99(N) --> num_10_19(N).
num_1_99(N) --> decade(T), opt_1_9(U), {N is T+U}.

num_100_999(N) --> num_1_9(H), [hundred], opt_1_99(S), {N is 100*H+S}.

opt_1_9(0) --> [].
opt_1_9(N) --> num_1_9(N).

opt_1_99(0) --> [].
opt_1_99(N) --> [and], num_1_99(N).

num_1_9(1) --> [one].   num_1_9(2) --> [two].   num_1_9(3) --> [three].
num_1_9(4) --> [four].  num_1_9(5) --> [five].  num_1_9(6) --> [six].
num_1_9(7) --> [seven]. num_1_9(8) --> [eight]. num_1_9(9) --> [nine].

num_10_19(10) --> [ten].        num_10_19(11) --> [eleven].
num_10_19(12) --> [twelve].     num_10_19(13) --> [thirteen].
num_10_19(14) --> [fourteen].   num_10_19(15) --> [fifteen].
num_10_19(16) --> [sixteen].    num_10_19(17) --> [seventeen].
num_10_19(18) --> [eighteen].   num_10_19(19) --> [nineteen].

decade(20) --> [twenty].        decade(30) --> [thirty].
decade(40) --> [forty].         decade(50) --> [fifty].
decade(60) --> [sixty].         decade(70) --> [seventy].
decade(80) --> [eighty].        decade(90) --> [ninety].

这是双向的(并且可以枚举所有数字):

This work both ways (and can enumerate all the numbers):

?- phrase(num_0_999(46), Name).
Name = [forty, six]
Yes (0.00s cpu, solution 1, maybe more)

?- phrase(num_0_999(N), [forty, six]).
N = 46
Yes (0.00s cpu, solution 1, maybe more)

[我最初使用 #=/2 约束而不是 is/2 来使代码在两种模式下都可以工作,但是@CapelliC 的帖子提醒说,通过将算术移到相应规则结束...]

[I had originally used a #=/2 constraint instead of is/2 to make the code work in both modes, but was reminded by @CapelliC's post that the same can be achieved in plain Prolog by moving the arithmetic to the end of the respective rules...]

这篇关于整数与其名称之间的关系 - Prolog的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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