2
\$\begingroup\$

As you can see from my predicate iterate, I am repeating Z is X-3 and Z is X-2, the scenarios are different if the value is 0 or greater than 0. thus why the duplicates,

Was wondering if there was a way round this

iterate([], []).           %Nothing to change
    iterate([1|T], T).
    iterate([X|T], [Z|T]) :-   % Change only the headT
        (   Z is X - 3
        ;
            Z is X - 2
        ),
        Z>0 .
    iterate([X|T], T) :-   % Change only the head
        (   Z is X - 3
        ;
            Z is X - 2
        ),
        Z is 0 .

    iterate([X|Xs], [X|Ys]) :-  %Keep the head and change something later
        iterate(Xs, Ys).
\$\endgroup\$
  • 1
    \$\begingroup\$ Can you change your question title to reflect what your code does \$\endgroup\$ – Siobhan Nov 23 '16 at 22:31
  • \$\begingroup\$ yeah sorry I'm new here @TolaniJaiye-Tikolo \$\endgroup\$ – Small Legend Nov 23 '16 at 22:33
  • 1
    \$\begingroup\$ It is still extremely unclear to me what this predicate is supposed to do. \$\endgroup\$ – Fatalize Nov 25 '16 at 14:28
  • \$\begingroup\$ If ~ is X - 3 and Z is 0, then the condition can be written simply as X = 3. Thus the whole branch can be written as iterate([X|T], T) :- (X = 3 ; X = 2)., or perhaps even simpler: iterate([3|T], T). iterate([2|T], T). which you seem to be already doing in iterate([1|T], T).. But it's really difficult to understand the desired result. \$\endgroup\$ – wvxvw Oct 24 '17 at 9:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.