Prolog Remove the elements between the first and the last elements in a list

后端 未结 1 1248
死守一世寂寞
死守一世寂寞 2021-01-26 11:43

I am trying to keep only the first element and the last element for a list which contains only consecutive integers.

For example:

?- remove([1,2,3,4,5],          


        
1条回答
  •  礼貌的吻别
    2021-01-26 12:16

    To solve this problem you have to handle different cases, here the solution then the comment:

    last([E],E).
    last([_|T],E):-
        last(T,E).
    
    remove([],[]).
    remove([A],[A]).
    remove([A,B],[A,B]).
    remove([A|B],[A,Last]):-
        last(B,Last).
    
    findElementsR([A,B],LT,LOT,LO):-
        B =< A,
        append(LT,[A],LT1),
        remove(LT1,LRem),
        append(LOT,[LRem,[B]],LO).
    
    findElementsR([A,B|T],LT,LOT,LO):-
        B =< A,
        append(LT,[A],LT1),
        remove(LT1,LRem),
        append(LOT,[LRem],LOT1),
        findElementsR([B|T],[],LOT1,LO).
    
    findElementsR([A,B],LT,LOT,LO):-
        B is A+1, %consecutive
        append(LT,[A,B],LTO),
        remove(LTO,RM),
        append(LOT,[RM],LO).
    
    findElementsR([A,B|T],LT,LOT,LO):-
        B is A+1, %consecutive
        append(LT,[A],LTO),
        findElementsR([B|T],LTO,LOT,LO).
    
    findElements(L,LO):-
        findElementsR(L,[],[],LO).
    
    ?- findElements([3,1,2,3,2,3,4,5,2,3,4],L).
    L = [[3], [1, 3], [2, 5], [2, 4]]
    false
    

    So, first of all, i've defined last/2 that simply, given a list as an input, returns the last element

    ?- last([1,2,3],L).
    L = 3
    

    Then with remove/3 i get the list composd by th first and the last element.

    findElements/2 is used to call findElementsR/4 (to make it tail recursive). findElements/4 finds a list of consecutive elements and then calls remove/2 to get the first and the last.

    0 讨论(0)
提交回复
热议问题