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],[])..

Was it helpful?

Solution

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

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

second-->[X], {number(X)}.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top