Prolog - Binary addition?

谁说我不能喝 提交于 2019-12-11 03:53:25

问题


I need to write a Prolog predicate which calculate the sum of 2 binary numbers represented in list. The lists are already reversed, for example ([0,1] base 2) = (2 base 10).

It should work with mode binary_plus(+,+,-), for example

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

and with mode binary_plus(-,-,+), for example

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

Im not allowed using cut sign,findall,negation,or if-then-else.

Here is my code:

is_binary([]).
is_binary([X]):- X is 1.
is_binary([X|Xs]):-
    append(_,[1],Xs),
    member(X,[0,1]),    
    is_binary(Xs).

binary_plus([],X,X):-
    is_binary(X).
binary_plus(X,[],X):- 
    is_binary(X).

binary_plus([0|Xs],[Y|Ys],[Y|Zs]):-
    binary_plus(Xs,Ys,Zs).
binary_plus([1|Xs],[0|Ys],[1|Zs]):-
    binary_plus(Xs,Ys,Zs).
binary_plus([1|Xs],[1|Ys],[0|Zs]):-
    binary_plus(Xs,[1],Ws),
    binary_plus(Ws,Ys,Zs).

I dont know where i'm wrong because there are some strange issues that i cant solve, so if someone could help me i would appreciate it. Thanks.


回答1:


Here my take on binary-addition-without-anything. I understand that you are not to use clpfd:

binary_plus(A,B,C) :- binary_plus_0(A,B,C).

binary_plus_0([],    [],    []).
binary_plus_0([],    [B|Bs],[B|Bs]).
binary_plus_0([A|As],[],    [A|As]).
binary_plus_0([A|As],[B|Bs],[C|Cs]) :- binary_plus_0(A,B,C,As,Bs,Cs).

binary_plus_0(0,0,0,As,Bs,Cs) :- binary_plus_0(As,Bs,Cs).
binary_plus_0(0,1,1,As,Bs,Cs) :- binary_plus_0(As,Bs,Cs).
binary_plus_0(1,0,1,As,Bs,Cs) :- binary_plus_0(As,Bs,Cs).
binary_plus_0(1,1,0,As,Bs,Cs) :- binary_plus_1(As,Bs,Cs).

binary_plus_1([],    [],    [1]).
binary_plus_1([],    [B|Bs],Cs)     :- binary_plus_0([1],[B|Bs],Cs).
binary_plus_1([A|As],[],    Cs)     :- binary_plus_0([A|As],[1],Cs).
binary_plus_1([A|As],[B|Bs],[C|Cs]) :- binary_plus_1(A,B,C,As,Bs,Cs).

binary_plus_1(0,0,1,As,Bs,Cs) :- binary_plus_0(As,Bs,Cs).
binary_plus_1(0,1,0,As,Bs,Cs) :- binary_plus_1(As,Bs,Cs).
binary_plus_1(1,0,0,As,Bs,Cs) :- binary_plus_1(As,Bs,Cs).
binary_plus_1(1,1,1,As,Bs,Cs) :- binary_plus_1(As,Bs,Cs).



回答2:


When describing lists, always consider using DCG notation. For example, in your case, consider writing this as:

:- use_module(library(clpfd)).

binary_addition(Xs, Ys, As) :-
        phrase(binary_addition_(Xs, Ys, 0), As).

binary_addition_([], [], 0)     --> [].
binary_addition_([], [], 1)     --> [1].
binary_addition_([X|Xs], [], C) --> binary_addition_([X|Xs], [C], 0).
binary_addition_([], [Y|Ys], C) --> binary_addition_([C], [Y|Ys], 0).
binary_addition_([X|Xs], [Y|Ys], C0) -->
        { [X,Y] ins 0..1,
          Sum #= X + Y + C0 },
        sum_carry(Sum, C),
        binary_addition_(Xs, Ys, C).

sum_carry(0, 0) --> [0].
sum_carry(1, 0) --> [1].
sum_carry(2, 1) --> [0].

Example queries and their solutions:

?- binary_addition([1,0],[0,1,1], Sum).
Sum = [1, 1, 1] .

?- binary_addition([1,1],[1,0,1], Sum).
Sum = [0, 0, 0, 1] .

?- binary_addition([0,1],[1,1], Sum).
Sum = [1, 0, 1] .

Notice that it also works in the other direction:

?- binary_addition(Xs, Ys, [1,1]).
Xs = [1, 1],
Ys = [] ;
Xs = [],
Ys = [1, 1] ;
Xs = [_G2510, 1],
Ys = [_G2522],
_G2510 in 0..1,
_G2510+_G2522#=1,
_G2522 in 0..1 ;
etc.

You can simply add a reverse/2 goal to binary_addition/3 if you want the reverse list.



来源:https://stackoverflow.com/questions/29380105/prolog-binary-addition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!