in reply to symbolic increment/decrement of roman numerals

Great. My only question is why is 49 translated to IL (and vice versa)? According to the rules I know, along with those of Math::Roman and Roman, this is illegal. And if it were legal, shouldn't IC, ID, and IM be legal, too?

Note: I understand the idea of the symbolic approach, so that such things as inc('IM') return M (so, in this sense, IM is "legal" input, but not the expected output for dec('M') --- this is what I mean by "legal"). I just thought that inc('IL') should return L as expected, but dec('L') should return XLIX. (Note, similarly, that decrementing D ten times returns CDXC and not XD.)

Replies are listed 'Best First'.
Re^2: symbolic increment/decrement of roman numerals
by PetaMem (Priest) on Jun 04, 2005 at 10:08 UTC

    Indeed, for decrement the snippet has wrong behaviour for these cases. It can be fixed by extending the regex ruleset slightly, which is left as an exercise to the reader.

    Clearly, if the snippet would behave correcting, i.e. dec('IL') should return XLVIII, this would require more "gist".

    Basically I tested the inc/dec subs by letting the one increment and the other decrement numbers and checked whether the result remained original. Like so:

    use strict; my $roman = 'I'; my $roman2; while(1) { $roman = &inc($roman); # print "$roman\n"; if($roman ne &dec(&inc($roman))) { print "MISMATCH for $roman\n"; print "\t",&inc($roman),"\n"; print "\t",&dec($roman),"\n"; } }

    Bye
     PetaMem
        All Perl:   MT, NLP, NLU

      Clearly, if the snippet would behave correcting, i.e. dec('IL') should return XLVIII, this would require more "gist".
      Actually, I applaud you in the fact that dec('IL') does return XLVIII.