/examples/rosetta_code/roman_numerals.rx
Forth | 28 lines | 24 code | 4 blank | 0 comment | 2 complexity | 137197098b94555d70c78b0cb5c1a89c MD5 | raw file
- This is a port of the Forth code; but returns a string rather than displaying
- the roman numerals. It only handles numbers between 1 and 3999.
- : vector ( ...n"- )
- here [ &, times ] dip : .data ` swap ` + ` @ ` do ` ; ;
- : .I dup @ ^buffer'add ;
- : .V dup 1 + @ ^buffer'add ;
- : .X dup 2 + @ ^buffer'add ;
- [ .I .X drop ]
- [ .V .I .I .I drop ]
- [ .V .I .I drop ]
- [ .V .I drop ]
- [ .V drop ]
- [ .I .V drop ]
- [ .I .I .I drop ]
- [ .I .I drop ]
- [ .I drop ]
- &drop
- 10 vector .digit
- : record ( an- )
- 10 /mod dup [ [ over 2 + ] dip record ] &drop if .digit ;
- : toRoman ( n-a )
- here ^buffer'set
- dup 1 3999 within 0 =
- [ "EX LIMITO!\n" ] [ "IVXLCDM" swap record here ] if ;