简体   繁体   English

Prolog二进制加法问题?

[英]Prolog Binary Addition Issue?

I have this piece of homework that I have to write in Prolog.The requirement is to write a piece of code that does binary addition such as: 我有这篇文章我必须在Prolog中编写。要求是编写一段代码来执行二进制加法,例如:

?- add([1,0,1],[1,1],X).
X = [0,0,0,1]

And so, this is the code that I came up with: 所以,这是我提出的代码:

add([],[], _).
add([],Y, Z) :- append([], Y, Z).
add(X,[], Z) :- append(X,[],Z).
add([HX|TX],[HY|TY], Z) :-
    HX = 1,
    HY = 1,
    add(TX,TY, Z1),
    add([1],Z1, Z2),
    append([0],Z2,Z),!.
add([HX|TX],[HY,TY], Z) :-
    HX = 0,
    HY = 1,
    add(TX,TY,Z1),
    append([1],Z1, Z),!.
add([HX|TX],[HY|TY], Z) :-
    HX = 1,
    HY = 0,
    add(TX,TY,Z1),
    append([1],Z1, Z),!.
add([HX|TX],[HY,TY], Z) :-
    HX = 0,
    HY = 0,
    add(TX,TY,Z1),
    append([0],Z1, Z),!.

It seems to do what I needed, however, there are strange issues with it that I can not understand, so if someone could guide me to what I have done wrong, I would be glad. 它似乎做了我需要的东西,然而,它有一些我无法理解的奇怪问题,所以如果有人可以指导我做错了什么,我会很高兴的。

Results: 结果:

?- add([1,1,1,1], [1,1],Z).
Z = [0, 1, 0, 0, 1]. % this is correct

?- add([1], [1],Z).
Z = [0, 1]. % this is correct

?- add([1,1,0,1], [1,1],Z).
Z = [0, 1, 1, 1]. % this is correct

?- add([1],[0],Y).
Y = [1|_G7100]. % there is an error here, but its not the big issue.

?- add([1,0,1], [1,1],Z).   
false. % no results are returned.

    104 ?- add([0], [1],Z).
    false. % no results returned either

Problem: Whenever there seems to be a 0 in the first binary list, under some conditions(still trying to figure them out), no results seems to be returned.but I can't seem to locate my error.Would be glad if someone can tell me what I did wrong. 问题:每当第一个二进制列表中似乎有一个0,在某些条件下(仍然试图弄清楚它们),似乎没有返回结果。但我似乎无法找到我的错误。如果有人会很高兴可以告诉我我做错了什么。

There are 3 mistakes: 有3个错误:

  • Rule #1: Should be add([],[],[]). 规则#1:应该add([],[],[]). instead of add([],[], _). 而不是add([],[], _). . This fails for equal-length lists. 这对于等长列表来说是失败的。
  • Rule #5 and #7: Should be add([HX|TX],[HY|TY], Z) instead of add([HX|TX],[HY,TY], Z) . 规则#5和#7:应该add([HX|TX],[HY|TY], Z)而不是add([HX|TX],[HY,TY], Z) This fails when the second list ( Y ) contains less than two elements. 当第二个列表( Y )包含少于两个元素时,这会失败。

Fix these and your code should run well: see here . 修复这些并且您的代码运行良好:请参阅此处

I see you use 7 different rules, since you put the binary arithmetic inside the rules. 我看到你使用7种不同的规则,因为你把二进制算术放在规则中。 You can do with 6 different rules plus a start rule as follows: 您可以使用6个不同的规则和一个启动规则,如下所示:

add2(AL, BL, CL) :-
   add2(AL, BL, 0, CL).

add2([A | AL], [B | BL], Carry, [C | CL]) :-
   X is (A + B + Carry),
   C is X rem 2,
   NewCarry is X // 2,
   add2(AL, BL, NewCarry, CL).
add2([], BL, 0, BL) :- !.
add2(AL, [], 0, AL) :- !.
add2([], [B | BL], Carry, [C | CL]) :-
   X is B + Carry,
   NewCarry is X // 2,
   C is X rem 2,
   add2([], BL, NewCarry, CL).
add2([A | AL], [], Carry, [C | CL]) :-
   X is A + Carry,
   NewCarry is X // 2,
   C is X rem 2,
   add2([], AL, NewCarry, CL).
add2([], [], Carry, [Carry]).

Here are some example runs: 以下是一些示例运行:

?- add2([1,1,1,1], [1,1],Z).
Z = [0,1,0,0,1]
?- add2([1], [1],Z).
Z = [0,1]
?- add2([1,1,0,1], [1,1],Z).
Z = [0,1,1,1]
?- add2([1],[0],Y).
Y = [1]
?- add2([1,0,1], [1,1],Z).
Z = [0,0,0,1]
?- add2([0], [1],Z).
Z = [1]

Main advantage of the above solution, less invocations of add2/4 and you could replace the //2 and rem 2, and do addition in some other number system as well. 上述解决方案的主要优点是,add2 / 4的调用较少,你可以替换// 2和rem 2,并在其他数字系统中添加。

PS: Code taken from here . PS:代码来自这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM