0

Here is some program for converting hex to binary, and I need to get in the result something like X = [1,1,1,1,0,0,0,0] for pred([f,0]).

So I need to join all lists in one. Can someone show me how to do it?

hex(0, [0,0,0,0]).
hex(1, [0,0,0,1]).
hex(2, [0,0,1,0]).
hex(3, [0,0,1,1]).
hex(4, [0,1,0,0]).
hex(5, [0,1,0,1]).
hex(6, [0,1,1,0]).
hex(7, [0,1,1,1]).
hex(8, [1,0,0,0]).
hex(9, [1,0,0,1]).
hex(a, [1,0,1,0]).
hex(b, [1,0,1,1]).
hex(c, [1,1,0,0]).
hex(d, [1,1,0,1]).
hex(e, [1,1,1,0]).
hex(f, [1,1,1,1]).

pred([],X).

pred([H|T],X):-
 hex(H,Y),
 append(X,Y,X),
 write(Y),
 nl,
 pred(T).

My problem is in append.

Will Ness
  • 70,110
  • 9
  • 98
  • 181
  • 1
    `append(X,Y,X)` says X is the result of X appended to Y. This could only ever be true if Y were empty. This tells me you think you can change the value of a variable in Prolog, but you cannot, you need another variable here. Also, intermixing of output with your logic is going to hurt you in the long run. I would recommend you consult a tutorial! You can't wing it in Prolog. – Daniel Lyons Jun 14 '18 at 19:23
  • see [How do I append lists in Prolog?](https://stackoverflow.com/q/11539203/849891), make sure you understand how `append` works with lists in a top-down manner. it applies here as well. not with your 'append' line, but with the `pred` predicate itself. `pred` will instantiate its binary list in the top-down manner, too. – Will Ness Jun 15 '18 at 16:48

1 Answers1

0

Let's start with some examples, pretending we already have this predicate:

?- pred( [f, 0],  X ).
X = [1,1,1,1, 0,0,0,0].        % that's what you said you want, isn't it?

?- pred( [f, 0],  [1,1,1,1, 0,0,0,0] ).
yes.

?- pred( [f | [0]],  [1,1,1,1 | [0,0,0,0]] ).
yes.

?- pred( [f | HEX], [A,B,C,D | BIN] ),  [A,B,C,D] = [1,1,1,1],  pred( HEX, BIN).
yes.

?- pred( [f | HEX], [A,B,C,D | BIN] ),  hex(f, [A,B,C,D]),  pred( HEX, BIN). 
yes.

?- pred( [0 | HEX2], [A2,B2,C2,D2 | BIN2] ),  hex(0, [A2,B2,C2,D2]),  pred( HEX2, BIN2). 
yes.

?- pred( [0 | HEX2], [A2,B2,C2,D2 | BIN2] ),  hex(0, [A2,B2,C2,D2]),  
                                               HEX2 = [], BIN2 = [],  pred( HEX2, BIN2). 
yes.

Yes? Right? (In case it isn't clear, consider the identity [0] = [0 | []]).

This means it must also be the case that

?- HEX2 = [], BIN2 = [],  pred( HEX2, BIN2). 
yes.

?- pred( [], []). 
yes.

Voila, we see what the base case must be. Right?

Moreover, we actually have seen there what the recursive case must be, as well. And you don't need any explicit append calling there. This:

?- pred( [f | HEX], [A,B,C,D | BIN] ),  hex(f, [A,B,C,D]),  pred( HEX, BIN). 
yes.

can be written as

?- pred( [F | HEX], [A,B,C,D | BIN] ),  F = f, hex(F, [A,B,C,D]),  pred( HEX, BIN). 
yes.

but actually, it is easily generalized to

?- pred( [F | HEX], [A,B,C,D | BIN] ),  hex(F, [A,B,C,D]),  pred( HEX, BIN). 
yes.

And there you have it.

See? Prolog is fun. Prolog is easy. Prolog is just saying what you mean.

Will Ness
  • 70,110
  • 9
  • 98
  • 181