如何在prolog中实现连接的原子列表?

发布于 2025-01-19 11:54:20 字数 765 浏览 0 评论 0原文

做了这个谓词,它确实有效,但我被要求在不使用内置函数的情况下实现原子列表 concat,这可能吗?

gradeInWords(Num,Words) :-
   WordsList = [zero,one,two,three,four,five,six,seven,eight,nine,ten,eleven,twelve,thirteen,fourteen,fifteen,sixteen,seventeen,eighteen,nineteen,twenty,thirty,forty,fifty,sixty,seventy,eighty,ninety,'one hundred'],
   (  Num < 20,
      nth0(Num,WordsList,Words),
      !
   ;  (  Tens is floor(Num/10),
         Ones is Num mod 10,
         (  Ones =:= 0,
            OnesWord = '',
            !
         ;  nth0(Ones,WordsList,OnesWord2),
            atomic_list_concat([",",OnesWord2],OnesWord)
         ),
         TPosition is Tens+19,
         nth1(TPosition,WordsList,TensWord)
      ),
      atomic_list_concat([TensWord,OnesWord],Words)
   ).

Made this predicate and it actually works but I am asked to implement atomic list concat without using built in function, is that possible ?

gradeInWords(Num,Words) :-
   WordsList = [zero,one,two,three,four,five,six,seven,eight,nine,ten,eleven,twelve,thirteen,fourteen,fifteen,sixteen,seventeen,eighteen,nineteen,twenty,thirty,forty,fifty,sixty,seventy,eighty,ninety,'one hundred'],
   (  Num < 20,
      nth0(Num,WordsList,Words),
      !
   ;  (  Tens is floor(Num/10),
         Ones is Num mod 10,
         (  Ones =:= 0,
            OnesWord = '',
            !
         ;  nth0(Ones,WordsList,OnesWord2),
            atomic_list_concat([",",OnesWord2],OnesWord)
         ),
         TPosition is Tens+19,
         nth1(TPosition,WordsList,TensWord)
      ),
      atomic_list_concat([TensWord,OnesWord],Words)
   ).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小兔几 2025-01-26 11:54:20

如果您的价值域非常有限,只需使用事实表!

gradeInWords(0,[zero]).
gradeInWords(1,[one]).
gradeInWords(2,[two]).
gradeInWords(3,[three]).
gradeInWords(4,[four]).
gradeInWords(5,[five]).
gradeInWords(6,[six]).
gradeInWords(7,[seven]).
gradeInWords(8,[eight]).
gradeInWords(9,[nine]).
gradeInWords(10,[ten]).
gradeInWords(11,[eleven]).
gradeInWords(12,[twelve]).
gradeInWords(13,[thirteen]).
gradeInWords(14,[fourteen]).
gradeInWords(15,[fifteen]).
gradeInWords(16,[sixteen]).
gradeInWords(17,[seventeen]).
gradeInWords(18,[eighteen]).
gradeInWords(19,[nineteen]).
gradeInWords(20,[twenty]).
gradeInWords(21,[twenty,one]).
gradeInWords(22,[twenty,two]).
gradeInWords(23,[twenty,three]).
gradeInWords(24,[twenty,four]).
gradeInWords(25,[twenty,five]).
gradeInWords(26,[twenty,six]).
gradeInWords(27,[twenty,seven]).
gradeInWords(28,[twenty,eight]).
gradeInWords(29,[twenty,nine]).
gradeInWords(30,[thirty]).
gradeInWords(31,[thirty,one]).
gradeInWords(32,[thirty,two]).
gradeInWords(33,[thirty,three]).
gradeInWords(34,[thirty,four]).
gradeInWords(35,[thirty,five]).
gradeInWords(36,[thirty,six]).
gradeInWords(37,[thirty,seven]).
gradeInWords(38,[thirty,eight]).
gradeInWords(39,[thirty,nine]).
gradeInWords(40,[forty]).
gradeInWords(41,[forty,one]).
gradeInWords(42,[forty,two]).
gradeInWords(43,[forty,three]).
gradeInWords(44,[forty,four]).
gradeInWords(45,[forty,five]).
gradeInWords(46,[forty,six]).
gradeInWords(47,[forty,seven]).
gradeInWords(48,[forty,eight]).
gradeInWords(49,[forty,nine]).
gradeInWords(50,[fifty]).
gradeInWords(51,[fifty,one]).
gradeInWords(52,[fifty,two]).
gradeInWords(53,[fifty,three]).
gradeInWords(54,[fifty,four]).
gradeInWords(55,[fifty,five]).
gradeInWords(56,[fifty,six]).
gradeInWords(57,[fifty,seven]).
gradeInWords(58,[fifty,eight]).
gradeInWords(59,[fifty,nine]).
gradeInWords(60,[sixty]).
gradeInWords(61,[sixty,one]).
gradeInWords(62,[sixty,two]).
gradeInWords(63,[sixty,three]).
gradeInWords(64,[sixty,four]).
gradeInWords(65,[sixty,five]).
gradeInWords(66,[sixty,six]).
gradeInWords(67,[sixty,seven]).
gradeInWords(68,[sixty,eight]).
gradeInWords(69,[sixty,nine]).
gradeInWords(70,[seventy]).
gradeInWords(71,[seventy,one]).
gradeInWords(72,[seventy,two]).
gradeInWords(73,[seventy,three]).
gradeInWords(74,[seventy,four]).
gradeInWords(75,[seventy,five]).
gradeInWords(76,[seventy,six]).
gradeInWords(77,[seventy,seven]).
gradeInWords(78,[seventy,eight]).
gradeInWords(79,[seventy,nine]).
gradeInWords(80,[eighty]).
gradeInWords(81,[eighty,one]).
gradeInWords(82,[eighty,two]).
gradeInWords(83,[eighty,three]).
gradeInWords(84,[eighty,four]).
gradeInWords(85,[eighty,five]).
gradeInWords(86,[eighty,six]).
gradeInWords(87,[eighty,seven]).
gradeInWords(88,[eighty,eight]).
gradeInWords(89,[eighty,nine]).
gradeInWords(90,[ninety]).
gradeInWords(91,[ninety,one]).
gradeInWords(92,[ninety,two]).
gradeInWords(93,[ninety,three]).
gradeInWords(94,[ninety,four]).
gradeInWords(95,[ninety,five]).
gradeInWords(96,[ninety,six]).
gradeInWords(97,[ninety,seven]).
gradeInWords(98,[ninety,eight]).
gradeInWords(99,[ninety,nine]).
gradeInWords(100,[hundred]).

请注意,atomic_list_concat/[2,3]仅与Swi-Prolog一起使用。

另外,对于您的任务,您不需要串联原子,您需要原子的列表

上面的代码使用所有序列系统运行,并且是双向的。

If your value domain is quite limited, simply use a table of facts!

gradeInWords(0,[zero]).
gradeInWords(1,[one]).
gradeInWords(2,[two]).
gradeInWords(3,[three]).
gradeInWords(4,[four]).
gradeInWords(5,[five]).
gradeInWords(6,[six]).
gradeInWords(7,[seven]).
gradeInWords(8,[eight]).
gradeInWords(9,[nine]).
gradeInWords(10,[ten]).
gradeInWords(11,[eleven]).
gradeInWords(12,[twelve]).
gradeInWords(13,[thirteen]).
gradeInWords(14,[fourteen]).
gradeInWords(15,[fifteen]).
gradeInWords(16,[sixteen]).
gradeInWords(17,[seventeen]).
gradeInWords(18,[eighteen]).
gradeInWords(19,[nineteen]).
gradeInWords(20,[twenty]).
gradeInWords(21,[twenty,one]).
gradeInWords(22,[twenty,two]).
gradeInWords(23,[twenty,three]).
gradeInWords(24,[twenty,four]).
gradeInWords(25,[twenty,five]).
gradeInWords(26,[twenty,six]).
gradeInWords(27,[twenty,seven]).
gradeInWords(28,[twenty,eight]).
gradeInWords(29,[twenty,nine]).
gradeInWords(30,[thirty]).
gradeInWords(31,[thirty,one]).
gradeInWords(32,[thirty,two]).
gradeInWords(33,[thirty,three]).
gradeInWords(34,[thirty,four]).
gradeInWords(35,[thirty,five]).
gradeInWords(36,[thirty,six]).
gradeInWords(37,[thirty,seven]).
gradeInWords(38,[thirty,eight]).
gradeInWords(39,[thirty,nine]).
gradeInWords(40,[forty]).
gradeInWords(41,[forty,one]).
gradeInWords(42,[forty,two]).
gradeInWords(43,[forty,three]).
gradeInWords(44,[forty,four]).
gradeInWords(45,[forty,five]).
gradeInWords(46,[forty,six]).
gradeInWords(47,[forty,seven]).
gradeInWords(48,[forty,eight]).
gradeInWords(49,[forty,nine]).
gradeInWords(50,[fifty]).
gradeInWords(51,[fifty,one]).
gradeInWords(52,[fifty,two]).
gradeInWords(53,[fifty,three]).
gradeInWords(54,[fifty,four]).
gradeInWords(55,[fifty,five]).
gradeInWords(56,[fifty,six]).
gradeInWords(57,[fifty,seven]).
gradeInWords(58,[fifty,eight]).
gradeInWords(59,[fifty,nine]).
gradeInWords(60,[sixty]).
gradeInWords(61,[sixty,one]).
gradeInWords(62,[sixty,two]).
gradeInWords(63,[sixty,three]).
gradeInWords(64,[sixty,four]).
gradeInWords(65,[sixty,five]).
gradeInWords(66,[sixty,six]).
gradeInWords(67,[sixty,seven]).
gradeInWords(68,[sixty,eight]).
gradeInWords(69,[sixty,nine]).
gradeInWords(70,[seventy]).
gradeInWords(71,[seventy,one]).
gradeInWords(72,[seventy,two]).
gradeInWords(73,[seventy,three]).
gradeInWords(74,[seventy,four]).
gradeInWords(75,[seventy,five]).
gradeInWords(76,[seventy,six]).
gradeInWords(77,[seventy,seven]).
gradeInWords(78,[seventy,eight]).
gradeInWords(79,[seventy,nine]).
gradeInWords(80,[eighty]).
gradeInWords(81,[eighty,one]).
gradeInWords(82,[eighty,two]).
gradeInWords(83,[eighty,three]).
gradeInWords(84,[eighty,four]).
gradeInWords(85,[eighty,five]).
gradeInWords(86,[eighty,six]).
gradeInWords(87,[eighty,seven]).
gradeInWords(88,[eighty,eight]).
gradeInWords(89,[eighty,nine]).
gradeInWords(90,[ninety]).
gradeInWords(91,[ninety,one]).
gradeInWords(92,[ninety,two]).
gradeInWords(93,[ninety,three]).
gradeInWords(94,[ninety,four]).
gradeInWords(95,[ninety,five]).
gradeInWords(96,[ninety,six]).
gradeInWords(97,[ninety,seven]).
gradeInWords(98,[ninety,eight]).
gradeInWords(99,[ninety,nine]).
gradeInWords(100,[hundred]).

Note that atomic_list_concat/[2,3] only works with SWI-Prolog.

Also, for your task you don't need concatenated atoms, you want lists of atoms.

The code above runs with all Prolog systems, and it is bi-directional.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文