Prolog Assigment for university

Hey everyone I have a problem in solving this assignment for my university Prolog class, if anyone can help me I will be grateful :slight_smile:


Thanks in advance.

Odd, that an exercise with a cut operator is provided as a first assignment, for bidirectional use of predicates.

Have a look at Markus Trishka’s online book: The Power of Prolog

To get some hints how to get started with Prolog.

maybe something like this?

symetric_encryp_decryp( 0, 1, 1 ):-!.
symetric_encryp_decryp( 1, 0, 1 ):-!.
symetric_encryp_decryp( 0, 0, 0 ):-!.
symetric_encryp_decryp( 1, 1, 0 ):-!.

xor( [ ], [ ], [ ] ):- !.
xor([ Xbit | Rest_text], [Ybit | Rest_key ], [Result_bit | Result_rest ] ):-
 symetric_encryp_decryp( Xbit, Ybit, Result_bit ),
  xor(  Rest_text,   Rest_key ,  Result_rest  ).
test:-
 xor( [1,1] , [0,1], Z ),
  write("result encrypt "),  write_term( Z, [] ), nl,
  xor( Z , [0,1], Y ),
  write("result decrypt "),  write_term( Y, [] ), nl.
1 Like

I think you can get rid of every single of the five (5) cuts in this code snippet without changing the behaviour of the program on SWI-Prolog.

It is a small mystery how and why you’d use the cut operator or append/3 to program this. Could you provide any additional info on that? Maybe there is something in this “SEAR module” (??) that would provide hints?

through Visual Prolog i am used to placing cuts everywhere to make minimum backtracks

Many Prolog systems have maplist/4, so you can also solve the problem:

xor_list(A, B, C) :- maplist(xor_elem, A, B, C).

xor_elem(A, B, C) :- C is xor(A,B).

Example run:

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

I did avoid making a predicate by the name xor/3, since there is no
guarantee that this works, xor/2 evaluable function is part of ISO core standard:
https://www.complang.tuwien.ac.at/ulrich/iso-prolog/dtc2#xor

But in some Prolog systems this means there is also a built-in xor/3 already.

1 Like

it seems that in this case, the cuts don’t add anything that indexing already determined – i guess, because the encrypt relationship is functional in both/all directions …

Thanks for your reply, can you elaborate your answer in plain English, so I can understand how it is solved. Again, thanks! :slight_smile: