Search code examples
listprologtrim

Trimming off uninitialized list values in prolog


I'm writing something in prolog and the way I used append, it ends up returning a list like [a,b,c|_]. Is there any standard predicate (or simple way) to cut off all the uninitialized/arbitrary values?

Edit to add: Length won't work because the list could be of an arbitrary length, I don't know what it's going to be ahead of time, otherwise I would have already used that to trim it.


Solution

  • You should check into why append is giving you a list like that. Because observe this behavior of append that fixes the problem you are seeing:

    ?- append([a,b,c|_], X, L).
    L = [a,b,c|X]
    ?- append([a,b,c|_], X, L), X=[].
    L = [a,b,c]