this is a follow up to my earlier demo (to_roman,which converts arabic numbers to roman numerals).
this app attempts to do the conversion in the opposite direction. as in the earlier app, i’ve also coded the process using functional programming methods - which means the whole task is divided into many smaller tasks,each one doing one function only.
here are the steps for converting MMCDLXIV to arabic numbers.
- make a list, currlist, of the values of each roman character → [1000,1000,100,500,50,10,1,5]
2 make a copy of the same, call it prevlist, => [1000,100,500,50,10,1,5,0] (drop the first and append 0).
3.reverse the 2 lists = so in effect, process the original from right to left.
- “zip” the the two lists together to create cplist consisting of pairs of corresponding values.
- the first number is the current roman value, the 2nd number is the prior roman value.
- create a new list using this logic: if the first number is less than the 2nd,negate it, else use that value
==> [5, -1, 10, 50, 500, -100, 1000, 1000]
- sum them up and you get 2464
i’ve used recursion in practically all the blocks. they take the place of loops but you have to be familiar with the technique to use it effectively.
there is no editing of the roman string invalid - i’m only processing codes that are (M,D,C,L.X.C.I) - all others are zeroes. i only recognize this simplified rule: if the left position value is less than its adjacent neighbor on the right, negate the value - so this app will process “IC” as 99, without any complaint.
here’s the aia
un_roman.aia (5.8 KB)