Search code examples
prologunification

Removing head prolog


check(Departure, Destination, [(D, S)|T1], [H|T2] ):-
    check(Departure, Destination, T1, T2),
    ((Destination = D, H = D); H = '').

I have the above code and I wanted to skip assigning H. I get a list of commas when I do that.


Solution

  • Your problem is that the fourth argument always "adds" an item to the list. If you want to "skip" that item altogether can avoid adding H to the head of that argument in that case.

    i.e.:

    check(Departure, Destination, [(D, S)|T1], NT2 ):-
        check(Departure, Destination, T1, T2),
        ((Destination = D, NT2=[D|T2]); NT2=T2).
    

    The clause states that the fourth argument should have D appended to T2 at the beginning when the recursive call to check/4 yields the same destination (the left part of the ; ), thus NT2=[D|T2] in that case. Otherwise, the fourth argument should be just what the recursive step leaves in T2, thus NT2=T2 in that case.

    Note also that S is a singleton in your code. If you really don't want to use S in that procedure then use _ instead of S