Prolog, forcing search without "fail".

I’m writing a program that classifies guitar chords. I have a predicate classify/1 which takes a list of notes and outputs a chord name. Currently, I’ve only been able to get it to produce multiple answers (i.e. Asus4 and Dsus2 for the same chord) by using “fail” at the end of all my classify/1 predicates, forcing the search. Is there another way which doesn’t cause the program to say “No” after every query?

It’s not really important, I’d just prefer it to say “Yes” instead of “No” :slight_smile:

Thanks in advance.

You need to use the cut operator which is a ! in Prolog IIRC. A ! will stop the search algorithm.

They say you can tell Prolog programmers who came from FORTRAN because they would just ! after every operation ;).

Cut stops the search, I want to force it.

Here’s some of my code:

classify(Notes) :-
  is_suspended_second(Notes, Root),
  write(X), write('sus2'), nl.
classify(Notes) :-
  is_suspended_fourth(Notes, Root),
  write(X), write('sus4'), nl.

Now, I call classify with a list of my notes, but without “fail” at the end of every classify predicate, it only prints out one answer, despite two being eligible for printing.

For example,
?- classify([‘A’, ‘D’, ‘E’]).
?- Asus4
?- Yes.

Instead of
?- classify([‘A’, ‘D’, ‘E’]).
?- Asus4
?- Dsus2
?- No.

Uh, it’s been a while but it looks like what’s happening is the is_suspended_second is reading in the entire list so the prolog search stops it’s backtrack.

What you want to do is something like:

classify() :- !.
classify(H|T) :- is_suspended_second(H), classify(T).

That way, each token gets passed.