質問

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

役に立ちましたか?

解決

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

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

second-->[X], {number(X)}.
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top