it may depend on what you are trying to do.
Are you trying (as it appears) to put in more than one stack element at
a time as in 1 2 enter puts 1 in "y" 1 in the "x "register?
I can see some advantage to this.
If you are trying to emulate something like an HP 11C (which I have in
front of me) then you have a different and probably simpler problem to
do. I have done it in Visual Basic-including trig and complex
numbers-and I know that J would do it better as it doesn't need a
subroutine to handle trig, logs etc with complex numbers..
the emulation involves single number entries where enter pushes the
stack up (x t0 y, y to z etc (duplicating x and discarding what was at
the top of the stack with the next entry replacing the x value.) with
operations on the x and y variables.
In other words enter has one function -this push up. and the +,- etc
works on the two bottom values, dropping the stack oppositely to enter
(duplicating the highers value and dropping z to y and the result of the
operation in x. -this is convenient in some cases).
Don Kelly
The following emulates a simple reverse Polish notation calculator which
operates on numbers at the bottom of a stack, replacing them by the result.
How would you do it? (Beware of line wrap.)
clear =: 3 : ',.STACK =: '''''
enter =: 4 : ',.STACK =: STACK , x' NB. x a list of numbers
chs =: 3 : ',.STACK =: (_1 }. STACK), - _1 {. STACK'
dup =: 4 : ',.STACK =: STACK , (- x) {. STACK' NB. x a non-negative
integer
plus =: 3 : ',.STACK =: ( _2 }. STACK), +/ _2 {. STACK'
minus =: 3 : ',.STACK =: ( _2 }. STACK), -/ _2 {. STACK'
times =: 3 : ',.STACK =: ( _2 }. STACK), */ _2 {. STACK'
divide =: 3 : ',.STACK =: ( _2 }. STACK), %/ _2 {. STACK'
clear''
1 2 enter ''
1
2
chs '' NB. Change sign of bottom number
1
_2
2 dup'' NB. Can use dup to keep a copy of numbers about to be combined
1
_2
1
_2
plus'' NB. 1 plus _2 is _1
1
_2
_1
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm