Kip,
The adverb is what you think it is, except you need to use a little trick
to workaround some limitations in the interpreter. Thus:
op1Arg =: adverb def 'y],.STACK =: (_1 }. STACK), u _1 {. STACK'
op2Arg =: adverb def 'y],.STACK =: ( _2 }. STACK), u/ _2 {. STACK'
Note y ] ... format; mentioning both y and u in an explicit adverb will
ensure the verb derived will also be explicit. If you don't mention y,
the interpreter makes a bad guess, and produces a tacit verb (which of
course can't update STACK).
Couple of other things: one, I wouldn't recommend calling the adverb
"monad" (foremost because it's incredibly confusing! but also because it
conflicts with the monad defined in the standard library); two, for
similar reasons, I wouldn't recommend naming a verb "clear" (maybe
"clearstack" instead)?
Anyway, see usage and examples below.
-Dan
clear =: 3 : ',.STACK =: '''''
NB. create and display empty STACK
enter =: 4 : ',.STACK =: STACK , x'
NB. x a list of numbers
dup =: 4 : ',.STACK =: STACK , (- x) {. STACK'
NB. x a non-negative integer
drop =: 4 : ',.STACK =: (- x) }. STACK'
chs =: - op1Arg
sqrt =: %: op1Arg
plus =: + op2Arg
minus =: - op2Arg
times =: * op2Arg
divide =: % op2Arg
clear'' NB. Initialize global vector STACK
5 2 enter''
5
2
1 dup''
5
2
2
times''
5
4
plus''
9
sqrt''
3
----- Original Message ---------------
Subject: [Jprogramming] rpn calculator
From: Kip Murray <[email protected]>
Date: Fri, 12 Sep 2014 08:17:42 -0500
To: "[email protected]" <[email protected]>
I'm proud of the Reverse Polish Notation calculator shown below (think HP
50g in rpn mode). My question is, can you devise an adverb monad so that
sqrt =: %: monad
produces the sqrt verb below?
Examples
clear'' NB. Initialize global vector STACK
5 2 enter''
5
2
1 dup''
5
2
2
times''
5
4
plus''
9
sqrt''
3
Verbs
clear =: 3 : ',.STACK =: '''''
NB. create and display empty STACK
enter =: 4 : ',.STACK =: STACK , x'
NB. x a list of numbers
dup =: 4 : ',.STACK =: STACK , (- x) {. STACK'
NB. x a non-negative integer
drop =: 4 : ',.STACK =: (- x) }. STACK'
NB. swap?
chs =: 3 : ',.STACK =: (_1 }. STACK), - _1 {. STACK'
sqrt =: 3 : ',.STACK =: (_1 }. STACK), %: _1 {. STACK'
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'
--Kip Murray
--
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