January 24th, 2006, 04:10 PM
Lisp: Roman Numerals
I was bored for a while Sunday and decided I'd write some Lisp that would take a roman numeral string and give the decimal integer value... just because I could.
So here it is, but I'd like it if anybody who is fluent in Lisp can point out any poor practices so that I no longer repeat them (I'm pretty novice).
[code=Lisp](defun romanval (x)
((eq x #\M) 1000)
((eq x #\D) 500)
((eq x #\C) 100)
((eq x #\L) 50)
((eq x #\X) 10)
((eq x #\V) 5)
((eq x #\I) 1)))
(defun strtolist (str)
(let ((l (length str)))
(do ((i 0 (+ i 1))
(lst nil (push (char str i) lst)))
((>= i l) (reverse lst)))))
(defun charstoroman (chars)
(labels ((rec (src dest)
((null src) (reverse dest))
(push (romanval (car src)) dest)
(rec (cdr src) dest)))))
(rec chars nil)))
(defun calcroman (lst)
(labels ((rec (src last sum)
((null src) sum)
(rec (cdr src) (car src)
(if (< (car src) last)
(- sum (car src))
(+ sum (car src))))))))
(rec (reverse lst) 0 0)))
(defun romantoi (str)
(calcroman (charstoroman (strtolist str))))[/code]
January 24th, 2006, 06:44 PM
I haven't got the time to look at this in detail right now but one thing that stands out right of the bat is naming conventions. Composite names in Lisp/Scheme are usually hyphenated ala something-and-something-else, this makes things easier to read . You may also want to get in the habit of commenting code you write.
I'll do my best to take a proper look at it tomorrow and recommend any improvements.
Good work though ,
January 24th, 2006, 06:52 PM
In the mean time you may like to know about the coerce function; it simply lets you forcefully change one type into another if at all possible.
I wrote these for a project I was working on, they're simple wrappers but they're worth keeping around .
(defun implode (list)
"The inverse of explode; implode returns a string representation of a list
(coerce list 'string))
(defun explode (string)
"The inverse of implode; explode returns a list representation of a string."
(coerce string 'list))
Last edited by netytan; January 24th, 2006 at 06:54 PM.
January 25th, 2006, 11:29 AM
I knew there had to be a built-in functions for conversions, I just need to find a reference to built-in functions. I'm using CLisp 2.37, btw. The only references I have at the moment are the tutorial doc that come with CLisp, On Lisp by Paul Graham (PDF), and a couple HTML pages I found.
January 25th, 2006, 02:38 PM
Make sure you have HyperSpec.
Comments on this post
January 25th, 2006, 03:11 PM
Heres a rewritten version of the code you posted. It's the same length with [lots of] comments so it should actually be shorter .
Note though that this is partially stylized, it's not usual for instance that the helper function be defined externally however it can make debugging easier. Aside from that it's this is more often done in Scheme where define/defun's can be nested or a named let can be easily used – kinda like labels but tidier .
(defun roman-digit->arabic (v)
"Converts a given single roman numeral into its equivalent arabic value."
;; Maps each character used in the roman number system onto their value.
;; Returns the value corresponding to V or NIL otherwise.
(defun roman->arabic-helper (rest last sum)
"A none-strict utility that converts roman numerals into equivalent arabic
numbers; works on a list of numbers :)."
(if (consp rest)
(let ((this (car rest))
(rest (cdr rest)))
;; Recursively calls roman->arabic-helper on the rest of the list; if
;; the last number was less than this one then subtract the new value,
;; else add them 0_o.
(roman->arabic-helper rest this
(funcall (if (< last this) ; Return and evaluate the +/- function.
sum)) ;Returns sum once the whole list has been processed.
(defun roman->arabic (v)
"Called to convert a roman numeral into it's fully expanded arabic form."
;; Calls roman-digit->arabic-value on each item of the newly coerced
;; list of characters.
(coerce v 'list))
I hope this helps man,
Comments on this post
Last edited by netytan; January 25th, 2006 at 03:19 PM.
January 25th, 2006, 04:14 PM
Well, it's shorter by default because the function that converted a string to a list of characters was cut in favor of (coerce string 'list).
Thanks for the link to HyperSpec. Now I just need to figure an easy way to download it so I have an offline copy. (I do most coding on my laptop, which I keep unconnected from networks most of the time).
January 25th, 2006, 04:53 PM
That and a lack of 'chartoroman . All in all we just need a function to check if the numerals are in a valid form before computing it to have a pretty tight implementation of Roman to arabic.
Either way I hope it was useful,
January 25th, 2006, 05:14 PM
That's a bit harder. Maybe with some Regular Expressions, but that's probably overkill. Perhaps on some day when I want to figure something out, but don't want to work on anything I should be working on, I'll take a go at it.
Originally Posted by netytan
January 27th, 2006, 03:46 PM
Well, I finally got around to taking a look at your rewrite last night, and it didn't work. Just three minor bugs, so I have to surmise from the googlie-eyes you had in the comment in roman->arabic-helper (O_o) that the algorithm I was using was not clear.
The first couple in roman->arabic-helper:
[code=Original] (funcall (if (< last this)
[code=Fixed] (funcall (if (< this last)
The third was that the results of the list that mapcar returns in roman->arabic needs to be reversed before it goes to roman->arabic-helper.
So, if we supply the string "MCMXLV" (just to supply some subtractions), then we get this list out of mapcar:
(1000 100 1000 10 50 5)
For the algorithm to work, that list has to be run from the lowest end, so we reverse it first:
(5 50 10 1000 100 1000)
Then we start with a sum of 0, adding most of the time, but if we encounter a value smaller than the last value, we subtract instead (CM = 1000 - 100, XL = 50 - 10):
5 > 0: sum += 5 -> 5
50 > 5: sum += 50 -> 55
10 < 50: sum -= 10 -> 45
1000 > 10: sum += 1000 -> 1045
100 < 1000: sum -= 100 -> 945
1000 > 100: sum += 1000 -> 1945
Comments on this post
Joel B Fant
"An element of conflict in any discussion is a very good thing. Shows everybody's taking part, nobody left out. I like that."
References: Threading in .NET
January 27th, 2006, 07:07 PM
sorry lion, I only tested it with a handful of values and it worked for them so I thought it was working, apparently not. The googlie eyes were actually because the function gave me a little trouble at first.
I had it working with <= but wanted to make that either < or > for conciseness; I didn't actually look at your code other than to see what it did.
Thanks for the fix man,
January 31st, 2006, 05:58 AM