Append difference zero to list in Prolog

2019-08-05 18:16发布

问题:

I need to take the difference of length of NUM1 and ANS and Num2 & ANS and append zero equal to the difference of length to both NUM1 and NUM2. I have trouble doing that.

sum(NUM1, NUM2, ANS):-
    comp(NUM1, ANS),
    comp(NUM2, ANS),
    % Arguments: Carry's from right and to left both 0 and all available digits
    add( NUM1, NUM2, ANS, 0, 0, [0,1,2,3,4,5,6,7,8,9], _).

add( [], [], [], C, C, Digits, Digits).
comp(NUM, ANS).

comp(NUM, ANS, OUT):-
    length(NUM, L1),
    length(ANS, L2),
    LI = [0],
    (L1 < L2 -> OUT = appendlist(LI, NUM, NUM)).

appendlist([],X,X).
appendlist([X|Y],Z,[X|W]) :- append(Y,Z,W).
标签: prolog