Prolog Battery Problem

Studying Prolog, I am trying to solve the following problem using batteries:

Write the predicate addone2 ​​/, whose first argument is a list of integers, and the second argument is a list of integers obtained by adding 1 to each integer in the first list. For example, request

       addone([1,2,7,2],X).

should give

       X = [2,3,8,3].

I created the following code:

addone([], _).
addone([E|Tail], [R|Rs]) :-
    NewE is E+1,
    append([R|Rs], [NewE], NewRs),
    addone(Tail, NewRs).

But it does not work. Can someone tell me why? So how can I use batteries in Prolog?

Thank!

+3
source share
2 answers

anthares , . , append. Prolog , , , , . :

addone([E|Tail], [E1|Rs]) :-
    E1 is E+1,
    addone(Tail, Rs).

, . E1 , . Rs . .

+4

addone([],[])., NewRs []

+1

Source: https://habr.com/ru/post/1776172/


All Articles