You have exposed a peculiarity in verbs plus minus times divide. When the stack has only one number they in effect precede the single number in the stack with a zero before acting.
minus plus 2 dup chs 1 2 enter clear '' NB. expected result 1 _1 times minus plus 2 dup chs 1 2 enter clear '' NB. expected result _1 divide times minus plus 2 dup chs 1 2 enter clear '' NB. you get the result of 0 % _1 0 On Thursday, June 5, 2014, Linda Alvord <[email protected]> wrote: > I'm not sure what you are looking for, but here's what I did: > > f=: 13 :',.S=: (_2 }. S) ,%/ _2 {. ,.S=: (_2 }. S),*/_2 {.,.S=: (_2 > }.S),-/_2 {.,.S=: (_2 }.S),+/_2 {.,.S=:S,(-x){.,.S=:(_1 }. S),-_1 > {.S=:,.(S=:''''),y' > > 2 f 1 2 > 0 > > divide times minus plus 2 dup chs 1 2 enter clear '' > 0 > > Linda > > > > -----Original Message----- > From: [email protected] <javascript:;> > [mailto:[email protected] <javascript:;>] On > Behalf Of Kip Murray > Sent: Thursday, June 05, 2014 1:46 AM > To: [email protected] <javascript:;> > Subject: [Jprogramming] rpn calculator > > 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 > > > > > > -- > Sent from Gmail Mobile > ---------------------------------------------------------------------- > For information about J forums see http://www.jsoftware.com/forums.htm > > ---------------------------------------------------------------------- > For information about J forums see http://www.jsoftware.com/forums.htm > -- Sent from Gmail Mobile ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm
