|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
|
|
#1
|
||||
|
||||
|
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)
(cond
((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)
(cond
((null src) (reverse dest))
(t
(push (romanval (car src)) dest)
(rec (cdr src) dest)))))
(rec chars nil)))
(defun calcroman (lst)
(labels ((rec (src last sum)
(cond
((null src) sum)
(t
(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))))
|
|
#2
|
||||
|
||||
|
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 ,Mark.
__________________
... > (define links (list google scheme ruby python others ...)) ; Read my blog at http://netytan.blogspot.com/. > _
|
|
#3
|
||||
|
||||
|
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.
Code:
(defun implode (list) "The inverse of explode; implode returns a string representation of a list object." (coerce list 'string)) (defun explode (string) "The inverse of implode; explode returns a list representation of a string." (coerce string 'list)) I wrote these for a project I was working on, they're simple wrappers but they're worth keeping around .Mark. Last edited by netytan : January 24th, 2006 at 05:54 PM. |
|
#4
|
||||
|
||||
|
Awesome.
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. |
|
#6
|
||||
|
||||
|
Heres a rewritten version of the code you posted. It's the same length with [lots of] comments so it should actually be shorter
.Code:
(defun roman-digit->arabic (v)
"Converts a given single roman numeral into its equivalent arabic value."
(case v
;; Maps each character used in the roman number system onto their value.
;; Returns the value corresponding to V or NIL otherwise.
(#\M 1000)
(#\D 500)
(#\C 100)
(#\L 50)
(#\X 10)
(#\V 5)
(#\I 1)))
(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.
#'-
#'+)
this sum)))
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."
(roman->arabic-helper
;; Calls roman-digit->arabic-value on each item of the newly coerced
;; list of characters.
(mapcar #'roman-digit->arabic
(coerce v 'list))
0 0))
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 .I hope this helps man, Mark. Last edited by netytan : January 25th, 2006 at 02:19 PM. |
|
#7
|
||||
|
||||
|
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). |
|
#8
|
||||
|
||||
|
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, Mark. |
|
#9
|
||||
|
||||
|
Quote:
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. |
|
#10
|
||||
|
||||
|
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)
#'-
#'+)
this sum)))
Code:
Fixed
(funcall (if (< this last)
#'-
#'+)
sum this)))
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 |
|
#11
|
||||
|
||||
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, Mark. |
|
#12
|
|||
|
|||
|
Quote:
see also Practical Common Lisp, Cliki and the CL Cookbook. On Lisp? Macro power to you! |