I am new in prolog so please don’t be to strict. I want to write a prolog program that given a list of numbers, returns a list such that the numbers in the input are replaced by the result of num2list, when they are followed by a bigger number, and otherwise skipped. When the list has only one number, this should also be replaced with the result of num2list. For example, if the input is [2,4,1,3,4,3,2,1], the result is [[0,1,2],[0,1],[0,1,2,3],[0,1]].
I know this doesn’t answer your question, but I strongly recommend “The Art of Prolog” by Sterling and Shapiro. It is much better than the tutorials I have found online. The book’s website now has a download link for the complete PDF.
It depends how deep you want to go, but the book is beginner-friendly, thorough, and well written. You don’t have to read it to the very end to make good use of it.
Once upon a time, findall/3, was the swiss knife of basic list processing. Coupled with nondeterministic accessors (like member/2, between/3, nth1/3, arg/3, append/3, etc etc) it provide common functionality in a fairly efficient and compact way.
There is an obvious bug, wrt the missing num2list of the last element. Next we’ll see how to fix that, refining the goal that is executed by findall/3, the 2nd argument.