Decode in Prolog using Peano

0

I'm doing an Prolog exercise that tries to decode a list.

decodifica([],[]).

decodifica([rlec(X,1)|L1],[X|L2]).
decodifica(L1,L2).

decodifica([rlec(X,N)|L1],[Xs|L1]):-
N1 is N-1, decodifica([rlec(X,N1)|L1],L2).

decodifica([E|L1],[E|L2]):- decodifica(L1,L2).

An example is:

?- decodifica([b,rlec(a,3)],R).

R = [b, a, a, a] ;

But I want to use Peano notation. Any suggestions?

    
asked by Mary 22.11.2016 в 22:31
source

1 answer

0

Assuming the following representation for the peano numbers:

  • zero for the number 0 .
  • succ(zero) for the number 1 .
  • ...
  • succ(N) for N+1 .

You simply have to change your current base case, 1 , to the first peano number, zero . Then, instead of subtracting 1 to N , parts of a succ(N) , from which you are removing successors:

decodifica([],[]).

decodifica([rlec(X,zero)|L1],[X|L2]).
decodifica(L1,L2).

decodifica([rlec(X,succ(N))|L1],[Xs|L1]):-
decodifica([rlec(X,N)|L1],L2).

decodifica([E|L1],[E|L2]):- decodifica(L1,L2).
    
answered by 24.11.2016 в 13:03