Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

THIS IS A PROLOG QUESTION ! Write a predicate chop_up(List, NewList) that takes

ID: 3702561 • Letter: T

Question

THIS IS A PROLOG QUESTION !

Write a predicate chop_up(List, NewList) that takes List and binds NewList to List with all sequences of successive increasing whole numbers replaced by a two-item list containing only the first and last number in the sequence. An example of successive increasing whole numbers is:19,20,21,22. (Note that the numbers have to be successive in the sense of increasing by exactly 1 at each step.) For example:

In this example, the sequence 9,10 has been replaced by [9,10], the sequence 5,6,7 has been replaced by [5, 7], and 2,3,4 has been replaced by[2, 4].

Explanation / Answer

succ([X],[X],[]).
succ([A,B| Xs],[A],[B|Xs]):-A + 1 == B.
succ([A,B|Xs],[A|X],R):-A + 1 =:= B,succ([B | Xs], X, R).
chopUp([],[]).
chopUp(L,[S|C]):-succ(L,S,R),chopUp(R, C).