<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: Roman Numerals</title>
	<atom:link href="http://programmingpraxis.com/2009/03/06/roman-numerals/feed/" rel="self" type="application/rss+xml" />
	<link>http://programmingpraxis.com/2009/03/06/roman-numerals/</link>
	<description>A collection of etudes, updated weekly, for the education and enjoyment of the savvy programmer</description>
	<lastBuildDate>Sat, 11 Feb 2012 09:48:16 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
	<item>
		<title>By: Dan Prager</title>
		<link>http://programmingpraxis.com/2009/03/06/roman-numerals/#comment-4297</link>
		<dc:creator><![CDATA[Dan Prager]]></dc:creator>
		<pubDate>Sun, 05 Feb 2012 20:31:33 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.wordpress.com/?p=150#comment-4297</guid>
		<description><![CDATA[A nice additional constraint to the problem -- at least for Roman numerals in additive form -- is to forbid conversion back to decimal for the purposes of carrying out the addition.  This seems more authentic, given that conversion to decimal was not an option available to the classical Romans!

I discuss this approach to a solution along with some other (arguably) interesting connections on my &lt;a href=&#039;http://ppprogramming.blogspot.com.au/2012/02/when-in-rome.html&#039; rel=&quot;nofollow&quot;&gt;new blog&lt;/a&gt;.  Straight to the &lt;a href=&#039;https://gist.github.com/1747170&#039; rel=&quot;nofollow&quot;&gt;Python code&lt;/a&gt;.]]></description>
		<content:encoded><![CDATA[<p>A nice additional constraint to the problem &#8212; at least for Roman numerals in additive form &#8212; is to forbid conversion back to decimal for the purposes of carrying out the addition.  This seems more authentic, given that conversion to decimal was not an option available to the classical Romans!</p>
<p>I discuss this approach to a solution along with some other (arguably) interesting connections on my <a href='http://ppprogramming.blogspot.com.au/2012/02/when-in-rome.html' rel="nofollow">new blog</a>.  Straight to the <a href='https://gist.github.com/1747170' rel="nofollow">Python code</a>.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Pavel Bogomolenko</title>
		<link>http://programmingpraxis.com/2009/03/06/roman-numerals/#comment-3940</link>
		<dc:creator><![CDATA[Pavel Bogomolenko]]></dc:creator>
		<pubDate>Fri, 02 Dec 2011 09:23:53 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.wordpress.com/?p=150#comment-3940</guid>
		<description><![CDATA[Hello everybody,

I briefly walked through all posted examples and figured out that noone completely solved this task. 
kawas was very close to solution however even in his second use case user=&gt; (add-roman &quot;MMCCCII&quot; &quot;MMDCII&quot;) result seemed to be wrong because there cannot be 4 M in a row. Please see wikipedia as a proof-link http://en.wikipedia.org/wiki/Roman_numerals
&lt;pre&gt;
The symbols &quot;I&quot;, &quot;X&quot;, &quot;C&quot;, and &quot;M&quot; can be repeated three times in succession, but no more. (They may appear four times if the third and fourth are separated by a smaller value, such as XXXIX.) &quot;D&quot;, &quot;L&quot;, and &quot;V&quot; can never be repeated.
&lt;/pre&gt;

So here is my solution in Python:

[sourcecode language=&quot;python&quot;]
rom_arab_dict = {&#039;I&#039; : 1, &#039;V&#039; : 5, &#039;X&#039; : 10, &#039;L&#039; : 50, &#039;C&#039; : 100, &#039;D&#039; : 500, &#039;M&#039; : 1000}
limit_sum = 3999

def add_roman(a, b): 
  arab_sum = roman_to_arabic(a) + roman_to_arabic(b)
  if arab_sum &lt;= limit_sum:
    arabic_to_roman(arab_sum)
    print(&#039;Roman sum: %s + %s = %s&#039; % (a, b, arabic_to_roman(arab_sum)))
    print(&#039;Arab sum(verification): %s + %s = %s&#039; % (roman_to_arabic(a), roman_to_arabic(b), arab_sum))
  else:
    print(&#039;Result sum out of range of Roman numbers.\n Should be less or equal to %d&#039; % limit_sum)

def roman_to_arabic(roman_num):
  sum = 0 
  arab_lst = [rom_arab_dict[x] for x in roman_num for y in rom_arab_dict.keys() if x == y]
  for index, item in enumerate(arab_lst):
    if index + 1 &lt; len(arab_lst):
      if item &lt; arab_lst[index + 1]: 
        arab_lst[index] = arab_lst[index + 1] - arab_lst[index]
        del arab_lst[index + 1]
    sum += arab_lst[index]

  return sum 
  
def arabic_to_roman(arab_num):
  # dict {&#039;key&#039;:val} to list [(v, k)]
  items = [(v, k) for k, v in rom_arab_dict.items()]
  items.sort()
  # reverse order from high to low
  items.reverse()

  rom_lst = []
  for index, item in enumerate(items):
    v, k = item
    while arab_num - v &gt;= 0:
      arab_num -= v
      tmp_v, tmp_k = items[index]
      rom_lst.append(tmp_k)
      if rom_lst.count(tmp_k) == 4:
        rom_lst = rom_lst[:-3]
        tmp_v, tmp_k = items[index - 1]
        rom_lst.append(tmp_k)
  
  return &#039;&#039;.join(rom_lst)

#Test cases
add_roman(&quot;CCCLXIX&quot;, &quot;CDXLVIII&quot;)
add_roman(&quot;CDXXVIII&quot;, &quot;DLXXVIII&quot;)
add_roman(&quot;MDCCL&quot;, &quot;MDCLXX&quot;)
[/sourcecode]

Results:

Roman sum: CCCLXIX + CDXLVIII = DCCCXVII
Arab sum(verification): 369 + 448 = 817
Roman sum: CDXXVIII + DLXXVIII = MVI
Arab sum(verification): 428 + 578 = 1006
Roman sum: MDCCL + MDCLXX = MMMCDXX
Arab sum(verification): 1750 + 1670 = 3420

Cheers,
Pavel]]></description>
		<content:encoded><![CDATA[<p>Hello everybody,</p>
<p>I briefly walked through all posted examples and figured out that noone completely solved this task.<br />
kawas was very close to solution however even in his second use case user=&gt; (add-roman &#8220;MMCCCII&#8221; &#8220;MMDCII&#8221;) result seemed to be wrong because there cannot be 4 M in a row. Please see wikipedia as a proof-link <a href="http://en.wikipedia.org/wiki/Roman_numerals" rel="nofollow">http://en.wikipedia.org/wiki/Roman_numerals</a></p>
<pre>
The symbols "I", "X", "C", and "M" can be repeated three times in succession, but no more. (They may appear four times if the third and fourth are separated by a smaller value, such as XXXIX.) "D", "L", and "V" can never be repeated.
</pre>
<p>So here is my solution in Python:</p>
<pre class="brush: python;">
rom_arab_dict = {'I' : 1, 'V' : 5, 'X' : 10, 'L' : 50, 'C' : 100, 'D' : 500, 'M' : 1000}
limit_sum = 3999

def add_roman(a, b):
  arab_sum = roman_to_arabic(a) + roman_to_arabic(b)
  if arab_sum &lt;= limit_sum:
    arabic_to_roman(arab_sum)
    print('Roman sum: %s + %s = %s' % (a, b, arabic_to_roman(arab_sum)))
    print('Arab sum(verification): %s + %s = %s' % (roman_to_arabic(a), roman_to_arabic(b), arab_sum))
  else:
    print('Result sum out of range of Roman numbers.\n Should be less or equal to %d' % limit_sum)

def roman_to_arabic(roman_num):
  sum = 0
  arab_lst = [rom_arab_dict[x] for x in roman_num for y in rom_arab_dict.keys() if x == y]
  for index, item in enumerate(arab_lst):
    if index + 1 &lt; len(arab_lst):
      if item &lt; arab_lst[index + 1]:
        arab_lst[index] = arab_lst[index + 1] - arab_lst[index]
        del arab_lst[index + 1]
    sum += arab_lst[index]

  return sum 

def arabic_to_roman(arab_num):
  # dict {'key':val} to list [(v, k)]
  items = [(v, k) for k, v in rom_arab_dict.items()]
  items.sort()
  # reverse order from high to low
  items.reverse()

  rom_lst = []
  for index, item in enumerate(items):
    v, k = item
    while arab_num - v &gt;= 0:
      arab_num -= v
      tmp_v, tmp_k = items[index]
      rom_lst.append(tmp_k)
      if rom_lst.count(tmp_k) == 4:
        rom_lst = rom_lst[:-3]
        tmp_v, tmp_k = items[index - 1]
        rom_lst.append(tmp_k)

  return ''.join(rom_lst)

#Test cases
add_roman(&quot;CCCLXIX&quot;, &quot;CDXLVIII&quot;)
add_roman(&quot;CDXXVIII&quot;, &quot;DLXXVIII&quot;)
add_roman(&quot;MDCCL&quot;, &quot;MDCLXX&quot;)
</pre>
<p>Results:</p>
<p>Roman sum: CCCLXIX + CDXLVIII = DCCCXVII<br />
Arab sum(verification): 369 + 448 = 817<br />
Roman sum: CDXXVIII + DLXXVIII = MVI<br />
Arab sum(verification): 428 + 578 = 1006<br />
Roman sum: MDCCL + MDCLXX = MMMCDXX<br />
Arab sum(verification): 1750 + 1670 = 3420</p>
<p>Cheers,<br />
Pavel</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: kawas</title>
		<link>http://programmingpraxis.com/2009/03/06/roman-numerals/#comment-3684</link>
		<dc:creator><![CDATA[kawas]]></dc:creator>
		<pubDate>Tue, 27 Sep 2011 22:17:04 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.wordpress.com/?p=150#comment-3684</guid>
		<description><![CDATA[At first I thought my solution was long and ugly and than I read the praxis&#039; Scheme solution :)
I read Roger&#039;s Scheme but it is incomplete : no encode-roman
I read FalconNL&#039;s Haskell and subtractiveStyle doesn&#039;t work on numbers like 1904

So I guess I&#039;ll roll my ugly clojure solution :
[sourcecode lang=&quot;css&quot;]
(def *romans* {\I 1, \V 5, \X 10, \L 50, \C 100, \D 500, \M 1000})

(defn from-roman [rs]
  (loop [rs (reverse rs) prev 0 dnum 0]
    (let [v (*romans* (first rs))]
      (cond
        (nil? v)   dnum
        (&lt; v prev) (recur (next rs) v (- dnum v))
        :else      (recur (next rs) v (+ dnum v))))))

(defn to-roman [d]
  (let [dq (quot d 1000) dr (rem d 1000)
        r (vec (repeat dq \M))
        romans (reverse (sort-by second *romans*))]
    (loop [d dr romans romans r r]
      (if (zero? d) (apply str r)
        (let [[[u10 v10] [u5 v5] [u1 v1]] romans dq (quot d v1) dr (rem d v1)]
          (cond
            (= dq 9) (recur dr (nnext romans) (conj r u1 u10))
            (&gt; dq 4) (recur dr (nnext romans) (apply conj r u5 (repeat (- dq 5) u1)))
            (= dq 4) (recur dr (nnext romans) (conj r u1 u5))
            (&gt; dq 0) (recur dr (nnext romans) (apply conj r (repeat dq u1)))
            :else    (recur d (nnext romans) r)))))))

(defn add-roman [&amp; rs]
  (to-roman (apply + (map from-roman rs))))
[/sourcecode]

Some use cases :
[sourcecode lang=&quot;css&quot;]
user=&gt; (add-roman &quot;CCCLXIX&quot; &quot;CDXLVIII&quot;)
&quot;DCCCXVII&quot;

user=&gt; (add-roman &quot;MMCCCII&quot; &quot;MMDCII&quot;)
&quot;MMMMCMIV&quot;
[/sourcecode]]]></description>
		<content:encoded><![CDATA[<p>At first I thought my solution was long and ugly and than I read the praxis&#8217; Scheme solution :)<br />
I read Roger&#8217;s Scheme but it is incomplete : no encode-roman<br />
I read FalconNL&#8217;s Haskell and subtractiveStyle doesn&#8217;t work on numbers like 1904</p>
<p>So I guess I&#8217;ll roll my ugly clojure solution :</p>
<pre class="brush: css;">
(def *romans* {\I 1, \V 5, \X 10, \L 50, \C 100, \D 500, \M 1000})

(defn from-roman [rs]
  (loop [rs (reverse rs) prev 0 dnum 0]
    (let [v (*romans* (first rs))]
      (cond
        (nil? v)   dnum
        (&lt; v prev) (recur (next rs) v (- dnum v))
        :else      (recur (next rs) v (+ dnum v))))))

(defn to-roman [d]
  (let [dq (quot d 1000) dr (rem d 1000)
        r (vec (repeat dq \M))
        romans (reverse (sort-by second *romans*))]
    (loop [d dr romans romans r r]
      (if (zero? d) (apply str r)
        (let [[[u10 v10] [u5 v5] [u1 v1]] romans dq (quot d v1) dr (rem d v1)]
          (cond
            (= dq 9) (recur dr (nnext romans) (conj r u1 u10))
            (&gt; dq 4) (recur dr (nnext romans) (apply conj r u5 (repeat (- dq 5) u1)))
            (= dq 4) (recur dr (nnext romans) (conj r u1 u5))
            (&gt; dq 0) (recur dr (nnext romans) (apply conj r (repeat dq u1)))
            :else    (recur d (nnext romans) r)))))))

(defn add-roman [&amp; rs]
  (to-roman (apply + (map from-roman rs))))
</pre>
<p>Some use cases :</p>
<pre class="brush: css;">
user=&gt; (add-roman &quot;CCCLXIX&quot; &quot;CDXLVIII&quot;)
&quot;DCCCXVII&quot;

user=&gt; (add-roman &quot;MMCCCII&quot; &quot;MMDCII&quot;)
&quot;MMMMCMIV&quot;
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: programmingpraxis</title>
		<link>http://programmingpraxis.com/2009/03/06/roman-numerals/#comment-27</link>
		<dc:creator><![CDATA[programmingpraxis]]></dc:creator>
		<pubDate>Tue, 10 Mar 2009 23:19:25 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.wordpress.com/?p=150#comment-27</guid>
		<description><![CDATA[FalconNL: Haskell is not one of the supported languages for the WordPress sourcecode tag.  See my HOWTO page for more about posting source code in comments.]]></description>
		<content:encoded><![CDATA[<p>FalconNL: Haskell is not one of the supported languages for the WordPress sourcecode tag.  See my HOWTO page for more about posting source code in comments.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: FalconNL</title>
		<link>http://programmingpraxis.com/2009/03/06/roman-numerals/#comment-26</link>
		<dc:creator><![CDATA[FalconNL]]></dc:creator>
		<pubDate>Tue, 10 Mar 2009 23:18:28 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.wordpress.com/?p=150#comment-26</guid>
		<description><![CDATA[Evidently there&#039;s only a limited selection of languages that will trigger the right formatting. My apologies.

[sourcecode lang=&#039;css&#039;]
import Data.Map (fromList, (!))
import Data.Char
import Data.List

data Roman = I &#124; V &#124; X &#124; L &#124; C &#124; D &#124; M deriving (Enum, Eq, Ord, Read, Show)

main = print $ addRoman &quot;CCCLXIX&quot; &quot;CDXLVIII&quot;

values :: [(Roman, Int)]
values = [(M, 1000), (D, 500), (C, 100), (L, 50), (X, 10), (V, 5), (I, 1)]

fromRoman :: String -&gt; Int
fromRoman = fromRoman&#039; . map (read . return) where
    fromRoman&#039; (x:y:xs) = (if x &lt; y then -1 else 1) * val x + fromRoman&#039; (y:xs)
    fromRoman&#039; xs = sum $ map val xs
    val c = fromList values ! c

toRoman :: Int -&gt; String
toRoman = map toLower . concatMap show . subtractiveStyle . toRoman&#039; values where
    toRoman&#039; []          _ = []
    toRoman&#039; ((r, v):xs) n = replicate (div n v) r ++ toRoman&#039; xs (mod n v)

subtractiveStyle :: [Roman] -&gt; [Roman]
subtractiveStyle (x:y:ys) &#124; y == pred x &amp;&amp; isPrefixOf [y,y,y] ys
                          = y : succ x : subtractiveStyle (drop 3 ys)
subtractiveStyle xs = xs

addRoman :: String -&gt; String -&gt; String
addRoman a b = toRoman $ fromRoman a + fromRoman b
[/sourcecode]]]></description>
		<content:encoded><![CDATA[<p>Evidently there&#8217;s only a limited selection of languages that will trigger the right formatting. My apologies.</p>
<pre class="brush: css;">
import Data.Map (fromList, (!))
import Data.Char
import Data.List

data Roman = I | V | X | L | C | D | M deriving (Enum, Eq, Ord, Read, Show)

main = print $ addRoman &quot;CCCLXIX&quot; &quot;CDXLVIII&quot;

values :: [(Roman, Int)]
values = [(M, 1000), (D, 500), (C, 100), (L, 50), (X, 10), (V, 5), (I, 1)]

fromRoman :: String -&gt; Int
fromRoman = fromRoman' . map (read . return) where
    fromRoman' (x:y:xs) = (if x &lt; y then -1 else 1) * val x + fromRoman' (y:xs)
    fromRoman' xs = sum $ map val xs
    val c = fromList values ! c

toRoman :: Int -&gt; String
toRoman = map toLower . concatMap show . subtractiveStyle . toRoman' values where
    toRoman' []          _ = []
    toRoman' ((r, v):xs) n = replicate (div n v) r ++ toRoman' xs (mod n v)

subtractiveStyle :: [Roman] -&gt; [Roman]
subtractiveStyle (x:y:ys) | y == pred x &amp;&amp; isPrefixOf [y,y,y] ys
                          = y : succ x : subtractiveStyle (drop 3 ys)
subtractiveStyle xs = xs

addRoman :: String -&gt; String -&gt; String
addRoman a b = toRoman $ fromRoman a + fromRoman b
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: FalconNL</title>
		<link>http://programmingpraxis.com/2009/03/06/roman-numerals/#comment-25</link>
		<dc:creator><![CDATA[FalconNL]]></dc:creator>
		<pubDate>Tue, 10 Mar 2009 23:13:21 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.wordpress.com/?p=150#comment-25</guid>
		<description><![CDATA[Haskell (someone more experienced than me can probably turn these into one-liners):

[sourcecode lang=&#039;haskell&#039;]
import Data.Map (fromList, (!))
import Data.Char
import Data.List

data Roman = I &#124; V &#124; X &#124; L &#124; C &#124; D &#124; M deriving (Enum, Eq, Ord, Read, Show)

main = print $ addRoman &quot;CCCLXIX&quot; &quot;CDXLVIII&quot;

values :: [(Roman, Int)]
values = [(M, 1000), (D, 500), (C, 100), (L, 50), (X, 10), (V, 5), (I, 1)]

fromRoman :: String -&gt; Int
fromRoman = fromRoman&#039; . map (read . return) where
    fromRoman&#039; (x:y:xs) = (if x  String
toRoman = map toLower . concatMap show . subtractiveStyle . toRoman&#039; values where
    toRoman&#039; []          _ = []
    toRoman&#039; ((r, v):xs) n = replicate (div n v) r ++ toRoman&#039; xs (mod n v)

subtractiveStyle :: [Roman] -&gt; [Roman]
subtractiveStyle (x:y:ys) &#124; y == pred x &amp;&amp; isPrefixOf [y,y,y] ys
                          = y : succ x : subtractiveStyle (drop 3 ys)
subtractiveStyle xs = xs

addRoman :: String -&gt; String -&gt; String
addRoman a b = toRoman $ fromRoman a + fromRoman b
[/sourcecode]]]></description>
		<content:encoded><![CDATA[<p>Haskell (someone more experienced than me can probably turn these into one-liners):</p>
<p>import Data.Map (fromList, (!))<br />
import Data.Char<br />
import Data.List</p>
<p>data Roman = I | V | X | L | C | D | M deriving (Enum, Eq, Ord, Read, Show)</p>
<p>main = print $ addRoman "CCCLXIX" "CDXLVIII"</p>
<p>values :: [(Roman, Int)]<br />
values = [(M, 1000), (D, 500), (C, 100), (L, 50), (X, 10), (V, 5), (I, 1)]</p>
<p>fromRoman :: String -&gt; Int<br />
fromRoman = fromRoman' . map (read . return) where<br />
    fromRoman' (x:y:xs) = (if x  String<br />
toRoman = map toLower . concatMap show . subtractiveStyle . toRoman' values where<br />
    toRoman' []          _ = []<br />
    toRoman' ((r, v):xs) n = replicate (div n v) r ++ toRoman' xs (mod n v)</p>
<p>subtractiveStyle :: [Roman] -&gt; [Roman]<br />
subtractiveStyle (x:y:ys) | y == pred x &amp;&amp; isPrefixOf [y,y,y] ys<br />
                          = y : succ x : subtractiveStyle (drop 3 ys)<br />
subtractiveStyle xs = xs</p>
<p>addRoman :: String -&gt; String -&gt; String<br />
addRoman a b = toRoman $ fromRoman a + fromRoman b</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Roger</title>
		<link>http://programmingpraxis.com/2009/03/06/roman-numerals/#comment-20</link>
		<dc:creator><![CDATA[Roger]]></dc:creator>
		<pubDate>Mon, 09 Mar 2009 19:56:18 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.wordpress.com/?p=150#comment-20</guid>
		<description><![CDATA[I started to program thinking that constructions like &quot;IIX&quot; for 8 are allowed. Wikipedia says that this kind of subtractive notation exists, but it seems very rare. Whatever, here is my roman-&gt;decimal.

[sourcecode lang=&#039;css&#039;]

(define roman-&gt;decimal
  (lambda (x)
    (decode-roman
     (string-&gt;list x))))

(define decode-roman
  (lambda (chars)
    (letrec
        ((decode-roman-helper
          (lambda (fir res akk cur)
            (cond
              ((null? res) (+ akk cur))
              ((&lt; (single-roman-&gt;decimal fir) (single-roman-&gt;decimal (car res))) 
               (decode-roman-helper (car res) 
                                    (cdr res)                                    
                                    (- akk cur)
                                    (single-roman-&gt;decimal (car res))))
              ((&gt; (single-roman-&gt;decimal fir) (single-roman-&gt;decimal (car res))) 
               (decode-roman-helper (car res) 
                                    (cdr res)                                    
                                    (+ cur akk)
                                    (single-roman-&gt;decimal (car res))))
              (else (decode-roman-helper 
                     (car res) 
                     (cdr res) 
                     akk 
                     (+ (single-roman-&gt;decimal (car res)) cur)))))))
      (decode-roman-helper (car chars) (cdr chars) 0 (single-roman-&gt;decimal (car chars))))))

(define single-roman-&gt;decimal
  (lambda (str)
    (cond
      ((char=? str #\M) 1000)
      ((char=? str #\D) 500)
      ((char=? str #\C) 100)
      ((char=? str #\L) 50)
      ((char=? str #\X) 10)
      ((char=? str #\V) 5)
      ((char=? str #\I) 1)
      (else 0))))
[/sourcecode]]]></description>
		<content:encoded><![CDATA[<p>I started to program thinking that constructions like &#8220;IIX&#8221; for 8 are allowed. Wikipedia says that this kind of subtractive notation exists, but it seems very rare. Whatever, here is my roman-&gt;decimal.</p>
<pre class="brush: css;">

(define roman-&gt;decimal
  (lambda (x)
    (decode-roman
     (string-&gt;list x))))

(define decode-roman
  (lambda (chars)
    (letrec
        ((decode-roman-helper
          (lambda (fir res akk cur)
            (cond
              ((null? res) (+ akk cur))
              ((&lt; (single-roman-&gt;decimal fir) (single-roman-&gt;decimal (car res)))
               (decode-roman-helper (car res)
                                    (cdr res)
                                    (- akk cur)
                                    (single-roman-&gt;decimal (car res))))
              ((&gt; (single-roman-&gt;decimal fir) (single-roman-&gt;decimal (car res)))
               (decode-roman-helper (car res)
                                    (cdr res)
                                    (+ cur akk)
                                    (single-roman-&gt;decimal (car res))))
              (else (decode-roman-helper
                     (car res)
                     (cdr res)
                     akk
                     (+ (single-roman-&gt;decimal (car res)) cur)))))))
      (decode-roman-helper (car chars) (cdr chars) 0 (single-roman-&gt;decimal (car chars))))))

(define single-roman-&gt;decimal
  (lambda (str)
    (cond
      ((char=? str #\M) 1000)
      ((char=? str #\D) 500)
      ((char=? str #\C) 100)
      ((char=? str #\L) 50)
      ((char=? str #\X) 10)
      ((char=? str #\V) 5)
      ((char=? str #\I) 1)
      (else 0))))
</pre>
]]></content:encoded>
	</item>
</channel>
</rss>

