如何在prolog中实现连接的原子列表?
做了这个谓词,它确实有效,但我被要求在不使用内置函数的情况下实现原子列表 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您的价值域非常有限,只需使用事实表!
请注意,
atomic_list_concat/[2,3]
仅与Swi-Prolog一起使用。另外,对于您的任务,您不需要串联原子,您需要原子的列表。
上面的代码使用所有序列系统运行,并且是双向的。
If your value domain is quite limited, simply use a table of facts!
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.