Search code examples
listprologpattern-matching

Prolog pattern match and return


New to Prolog here and already found it hard.

And here's the question: let's say I got a char list such as [h,e,l,l,o] and a unicode number 108(which is the letter "l").

I'm trying to write a function that take the elements of the list one by one and pattern match with the given unicode number. Once matched, return the reste of the list.

Worked on it for a whole day now , almost tried everything I can think of and I still didn't figure out how to make it work. Anyone got some ideas?


Solution

  • function([C|Chars], CutoffCode, Chars) :-
        char_code(C, CutoffCode).
    
    function([C|Chars], CutoffCode, Result) :-
        not(char_code(C, CutoffCode)),
        function(Chars, CutoffCode, Result).
    

    e.g.

    ?- function([h,e,l,l,o], 108, Result).
    Result = [l, o]
    
    ?- function([h,e,l,l,o], 109, Result).
    false