Monday 15 July 2013

math - Prolog Lists, Add 1 to tail of the list -


I am trying to add numbers 1 or 2 in a list in SWI-Prolog.

I have been able to add 1 or 2 at the top of the list, but it is difficult to add to each element on the tail of the list. I do not want to add each element 1 or 2 at the same time at different times. To wit. If my input is additive ([2,3,4], x).

I would like the following possibilities of X: X = [3,3,4] X = [4,3,4] X = [2,4,4] X = [2,5,4] X = [2,3,5] X = [2,3,6]

My code is currently:

<> [] [],) Add> . Addition ([H1 | T1], [H2 | T2]) is: - (H2, + (H1 1)), T1 = T2; Obviously it only adds 1 or 2 at the top of the list and not the tail. (H2, + (H1, 2)), T1 = T2

So does anyone know how I can go about adding 1 or 2 elements to the tail of my list?

Addition ([H1 | T], [H2 | T]): - H2H1 + 1 is addition ([H1 | T], [H2 | T]): - H2H1 + 2 Addition ([H + T], [H + T2]): - Add (T1, T2).

Now the options are listed, the last one simply handles - recursive - the remaining element

Anyway, your code is not just a line:

<( [] [],) Add> . Addition ([H1 | T1], [H2 | T2]) is: - (H2, + (H1 1)), T1 = T2; Is (H2, + (H1, 2)), T1 = T2; H1 = H2, plus (T1, T2).

After comment, there is a way to subtract here and keep only positive values:

  add ([H1 | T1], [H2 | T2]): - H2H1-1, H2 & gt; 0, T1 = T2; ...  

No comments:

Post a Comment