Examples/Roman

From NARS2000
Jump to: navigation, search
Examples - Go back to examples

The following function takes a decimal number and returns the equivalent in roman numerals. The comments to the right of the ⍝ symbol are for edification but are not necessary to use this function. This code is very simple in order to demonstrate the various operators in APL, and is not necessarily the most efficient way to implement this function.
      ⎕vr 'roman' ⍝ System function to list the contents of a function
    ∇ result ← roman value ⍝ result is the output of the function; value is the number supplied
[1]   result ← '' ⍝ set result to null to prevent VALUE ERROR
[2]   M: →(value<1000)/CM ⍝ if the value is less than 1000, go to label CM
[3]   result ← result,'m' ⍝ for each 1000, tack an 'm' onto result and reduce value by 1000
[4]   value ←value-1000
[5]   →M ⍝ go back to label M
[6]   CM: →(value<900)/D ⍝ if the value is less than 900, go to label D
[7]   result ← result,'cm' ⍝ tack a 'cm' on result and reduce value by 900
[8]   value ←value-900
[9]   D: →(value<500)/CD ⍝ if the value is less than 500, go to label CD
[10]  result ← result,'d'
[11]  value ←value-500
[12]  CD: →(value<400)/C ⍝ if the value is less than 400, go to label C
[13]  result ← result,'cd'
[14]  value ←value-500
[15]  C: →(value<100)/XC ⍝ if the value is less than 100, go to label XC
[16]  result ← result,'c'
[17]  value ←value-100
[18]  →C
[19]  XC: →(value<90)/L ⍝ if the value is less than 90, go to label L
[20]  result ← result,'xc'
[21]  value ←value-90
[22]  L: →(value<50)/XL ⍝ if the value is less than 50, go to label XL
[23]  result ← result,'l'
[24]  value ←value-50
[25]  XL: →(value<40)/X ⍝ if the value is less than 40, go to label X
[26]  result ← result,'xl'
[27]  value ←value-40
[28]  X: →(value<10)/IX ⍝ if the value is less than 10, go to label IX
[29]  result ← result,'x'
[30]  value ←value-10
[31]  →X
[32]  IX: →(value<9)/V ⍝ if the value is less than 9, go to label V
[33]  result ← result,'ix'
[34]  value ←value-9
[35]  V: →(value<5)/IV ⍝ if the value is less than 5, go to label IV
[36]  result ← result,'v'
[37]  value ←value-5
[38]  IV: →(value<4)/I ⍝ if the value is less than 4, go to label I
[39]  result ← result,'iv'
[40]  value ←value-4
[41]  I: →(value<1)/0 ⍝ if the value is less than 1, go to 0, which exits the function
[42]  result ← result,'i'
[43]  value ←value-1
[44]  →I
    ∇ 2013 3 18 11 42 9 262 (UTC) ⍝ this is when the function was last edited



See Also
System Commands System Variables and Functions Operators
Keyboard
A+S
Alt ¨ ¯ < > × ÷
Sh ~ ! @ # $ % ^ & * ( ) _ +
Key ` 1 2 3 4 5 6 7 8 9 0 - =
A+S
Alt ? π
Sh Q W E R T Y U I O P { } |
Key q w e r t y u i o p [ ] \
A+S §
Alt
Sh A S D F G H J K L : "
Key a s d f g h j k l ; '
A+S
Alt
Sh Z X C V B N M < > ?
Key z x c v b n m , . /
NARS 2000 Lang
Tool
Bar
+ - × ÷ * ! ? |
= < >
~ § π .. ,
/ \ ¨ .
_ ¯ b e g p r v x