Question

I have the following Prolog code:

expression-->first,operator,second.
first-->[X].
operator-->['+'];['-'].
second-->[X].

After compilation, the machine responds with "yes" in the command line for the following types of queries:

| ?- expression([5,'+',3],[]).
| ?- expression(['a','-','b'],[]).
| ?- expression([2.8,'+',3.2],[]).

How do I limit this to numbers only? Therefore, only expressions involving numbers should be truthful, like this one: | ?- expression([92,'+',23.48],[])..

Était-ce utile?

La solution

You can check whether X in first and second are numbers:

first-->[X], {number(X)}.

second-->[X], {number(X)}.
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top