1 ! Copyright (C) 2007 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: arrays assocs kernel math math.order math.vectors namespaces
4 quotations sequences sequences.lib sequences.private strings unicode.case ;
8 : roman-digits ( -- seq )
9 { "m" "cm" "d" "cd" "c" "xc" "l" "xl" "x" "ix" "v" "iv" "i" } ;
11 : roman-values ( -- seq )
12 { 1000 900 500 400 100 90 50 40 10 9 5 4 1 } ;
14 ERROR: roman-range-error n ;
16 : roman-range-check ( n -- )
17 dup 1 3999 between? [ drop ] [ roman-range-error ] if ;
19 : roman<= ( ch1 ch2 -- ? )
20 [ 1string roman-digits index ] bi@ >= ;
23 1string roman-digits index roman-values nth ;
26 roman-values roman-digits [
27 >r /mod swap r> <repetition> concat %
30 : (roman>) ( seq -- n )
31 dup [ roman>n ] map swap all-eq? [
39 dup roman-range-check [
43 : >ROMAN ( n -- str ) >roman >upper ;
46 >lower [ roman<= ] monotonic-split [
51 : 2roman> ( str1 str2 -- m n )
54 : binary-roman-op ( str1 str2 quot -- str3 )
55 >r 2roman> r> call >roman ; inline
58 : roman+ ( str1 str2 -- str3 )
59 [ + ] binary-roman-op ;
61 : roman- ( str1 str2 -- str3 )
62 [ - ] binary-roman-op ;
64 : roman* ( str1 str2 -- str3 )
65 [ * ] binary-roman-op ;
67 : roman/i ( str1 str2 -- str3 )
68 [ /i ] binary-roman-op ;
70 : roman/mod ( str1 str2 -- str3 str4 )
71 [ /mod ] binary-roman-op >r >roman r> ;