Reference no: EM13542262
Prolog Program: The predicate should take two arguments: a list corresponding to the program to execute, and a list corresponding to the contents of the resulting stack.
If any error occurs in parsing or executing the program (including stack underflow or unrecognized command words),the predicate should simply fail, in the Prolog sense.
example: main([1,2,4,+,*,9],X).
X=[12,9]
the way it is done is: when a + is encountered, the first two elements of the list are taken out and are added and the result is put back in the list, the list now becomes [3,4,*,9].
Further a * is encountered, hence it should multiply 3 and 4 and 12 should be added to the list, the list now becomes [12,9] since there are only integers remaining in the list [12,9] is the final result.
The predicate should fail if list is empty, or for the following examples,
[12,+]-not enough elements to do the operation,
[+,-], no integers
etc
the following are the operations that are needed to be implemented
+ ( n1 n2 -- sum ) Adds (n1 + n2).
- ( n1 n2 -- diff ) Subtracts (n1 - n2).
* ( n1 n2 -- prod ) Multiplies (n1 * n2).
/ ( n1 n2 -- quot ) Divides (n1 / n2), using integer division (truncates).
/MOD ( n1 n2 -- rem quot ) Divides. Returns the remainder and quotient.
MOD ( n1 n2 -- rem ) Returns the remainder from division.
= ( n1 n2 -- equal ) Compares for equality (n1 == n2). Indicates false with the value zero, and true with the value -1.