Search code examples
listprologdefinitionbacktracking

Prolog exercise with Lists


I would be grateful if someone help me with the underneath exercise

If I have the prolog predicate **split_list(Limit,List,High,Low) (split_list/4)** which has an integer list List, and one integer Limit, "returns" to list High all the data of list List which are greater or equal to Limit and the list Low which data are less of Limit. For instance: ?- split_list(2, [0, 2, 1, 3, 4], High, Low). High = [2, 3, 4] Low = [0, 1]

i) Give the backtracking definition of the predicate with less possible controls.

ii)Define a predicate split_list_alt/3 with the same declarative meaning of [i)] (non Recursion definition) //annotation:use predicates collection solutions(non backtracking)For instance: ?- split_list(0, [0, 2, 1, 3, 4], High, Low). High = [0, 2, 1, 3, 4] Low = [] Yes


Solution

  • You can use the following list of predicates to achieve this.

    split_list(_,[],[],[]):-
        !.
    
    split_list(N,[LH|LT],[LH|AT],B):-
        LH>=N,!,
        split_list(N,LT,AT,B).
    
    split_list(N,[LH|LT],A,[LH|BT]):-
        split_list(N,LT,A,BT).