整数与其名称之间的关系

编程入门 行业动态 更新时间:2024-10-27 05:24:17
本文介绍了整数与其名称之间的关系 - 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...]

更多推荐

整数与其名称之间的关系

本文发布于:2023-10-26 08:06:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1529582.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:整数   名称   关系

发布评论

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

>www.elefans.com

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