简体   繁体   中英

Delete the last item in a list using conc operation in prolog

How to write a prolog program to delete the last item in a list using conc operation.

cut_last( L1, L2)

Following is my answer and can somebody please correct me if I am wrong or is this correct?

cut_last(L1,L2):- conc(L2,[_],L1)

You can use append/3 to achieve that.

cut_last(L1, L2):-
    append(L2, [_], L1).

The clause states that L2 is L1 without its last element if appending any list of length one to L2 you will get something that unifies with L1 .

?- cut_last([1,2,3],L).
L = [1, 2] ;
false.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM