Goal:
Filter out all words in a list not starting with the specified character. The words starting with specified character should be stored in a new list without its first character.
Problem:
If all words in the list are allowed, it works as intended. When a word in a list is not allowed, the check fails (as expected) but it exits the predicate without attempting to proceed with the next word in the list (not expected), as in backtracking and attempting to redo the following word.
filter_word([Char|Rest], Char, Rest).
filter([], _, []).
filter([Word|Words], Char, [H|T]) :-
filter_word(Word, Char, H),
filter(Words, Char, T).
This is a homework assignment.
That’s because there’s no clause in
filterto handle the case where the word does not match the filter. There are three cases in this problem:You should write three clauses, accordingly.