将List转为数字,将数字加1,然后将数字转为列表
我的头被困在序言中的这个练习中,我一直在尝试自己做,但它就是行不通。示例: ?-后继([1,9,9],X) -> X = [2,0,0]。首先尝试反转列表并将其增加 1,然后执行 if %10 = 0 下一个元素也应该增加。问题是我太习惯编程语法了,我无法理解这一点。任何帮助将不胜感激。
到目前为止我已经这样做了,但输出是错误的。
%[1,9,9] -> 199 +1 -> 200;
numbers_atoms([],[]).
numbers_atoms([X|Y],[C|K]) :-
atom_number(C, X),
numbers_atoms(Y,K).
%([1,2,3],X)
digits_number(Digits, Number) :-
numbers_atoms(Digits, Atoms),
number_codes(Number, Atoms).
number_tolist( 0, [] ).
number_tolist(N,[A|As]) :-
N1 is floor(N/10),
A is N mod 10,
number_tolist(N1, As).
addOne([X],[Y]):-
digits_number(X,Y1), %[1,9,9] -> 199
Y1 is Y1+1, % 199 -> 200
number_tolist(Y1,[Y]), % 200 -> [2,0,0]
!.
I have my head stuck in this exercise in prolog, I ve been trying to do it on my own but it just won't work. Example: ?-succesor([1,9,9],X) -> X = [2,0,0]. Had tried first to reverse the list and increment it with 1 and then do a if %10 = 0 the next element should be incremented too. Thing is that I m too used with programming syntax and I can't get my head wrapped around this.Any help would be appreciated.
I have done this so far, but the output is false.
%[1,9,9] -> 199 +1 -> 200;
numbers_atoms([],[]).
numbers_atoms([X|Y],[C|K]) :-
atom_number(C, X),
numbers_atoms(Y,K).
%([1,2,3],X)
digits_number(Digits, Number) :-
numbers_atoms(Digits, Atoms),
number_codes(Number, Atoms).
number_tolist( 0, [] ).
number_tolist(N,[A|As]) :-
N1 is floor(N/10),
A is N mod 10,
number_tolist(N1, As).
addOne([X],[Y]):-
digits_number(X,Y1), %[1,9,9] -> 199
Y1 is Y1+1, % 199 -> 200
number_tolist(Y1,[Y]), % 200 -> [2,0,0]
!.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以像手动解决这个问题一样解决这个问题:遍历数字列表,直到到达最右边的数字;增加该数字并计算进位数字,该数字必须递归地向左传播。最后,如果等于 1,则在前面添加进位数字(否则忽略它)。
示例:
You can solve this problem similarly to how you would solve it manually: traverse the list of digits until you reach the rightmost digit; increment that digit and compute the carry-on digit, which must be recursively propagated to the left. At the end, prepend the carry-on digit if it is equal to 1 (otherwise, ignore it).
Examples:
这是一个不使用
is
的版本,并且可以两种方式工作:例如,
尽管在“向前”运行时它具有很好的确定性,但令人烦恼的是,如果“向后”运行它会找到答案,然后留下一个选择点如果重试该选择点,则无限循环。我认为导致这种情况的原因是从较高的数字开始,然后
reverse(ListIn, ListInRev)
没有什么可处理的,并开始生成越来越长的列表,列表中都充满了空变量,并且永远不会结束。我可以将输入和输出限制为
same_length/2
但我想不出一种方法来将它们限制为相同的长度或 ListOut 长一项 ([9,9,9] -> [1,0,0,0])。Here's a version which doesn't use
is
and can work both ways:e.g.
although it's nicely deterministic when run 'forwards', it's annoying that if run 'backwards' it finds an answer, then leaves a choicepoint and then infinite loops if that choicepoint is retried. I think what causes that is starting from the higher number then
reverse(ListIn, ListInRev)
has nothing to work on and starts generating longer and longer lists both filled with empty variables and never ends.I can constrain the input and output to be
same_length/2
but I can't think of a way to constrain them to be the same length or ListOut is one item longer ([9,9,9] -> [1,0,0,0]).这个答案试图改进@TessellatedHacker之前的答案,如下所示:
no_longer_than/2如下。请注意与
same_length/2
的相似之处:以下示例查询仍然确定性地成功,就像以前一样:
successor/2
的“向后运行”使用现在也普遍终止:This answer tries to improve the previous answer by @TessellatingHacker, like so:
The definition of
no_longer_than/2
follows. Note the similarity tosame_length/2
:The following sample queries still succeed deterministically, as they did before:
The "run backwards" use of
successor/2
now also terminates universally: