delphi programming forums mysql charset mget recursive synonimos
free ventrilo servers hosting cs javascript delay python find in list
Back Forum New
abstract:

(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))))


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
  1. (defun romanval (x)
  2.   (cond
  3.     ((eq x #\M) 1000)
  4.     ((eq x #\D) 500)
  5.     ((eq x #\C) 100)
  6.     ((eq x #\L) 50)
  7.     ((eq x #\X) 10)
  8.     ((eq x #\V) 5)
  9.     ((eq x #\I) 1)))
  10. (defun strtolist (str)
  11.   (let ((l (length str)))
  12.     (do ((i 0 (+ i 1))
  13.          (lst nil (push (char str i) lst)))
  14.         ((>= i l) (reverse lst)))))
  15. (defun charstoroman (chars)
  16.   (labels ((rec (src dest)
  17.              (cond
  18.                ((null src) (reverse dest))
  19.                (t
  20.                  (push (romanval (car src)) dest)
  21.                  (rec (cdr src) dest)))))
  22.     (rec chars nil)))
  23. (defun calcroman (lst)
  24.   (labels ((rec (src last sum)
  25.              (cond
  26.                ((null src) sum)
  27.                (t
  28.                  (rec (cdr src) (car src)
  29.                    (if (< (car src) last)
  30.                        (- sum (car src))
  31.                        (+ sum (car src))))))))
  32.     (rec (reverse lst) 0 0)))
  33. (defun romantoi (str)
  34.   (calcroman (charstoroman (strtolist str))))
Copy Code

TOP

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.

TOP

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:
  1. (defun implode (list)
  2.   "The inverse of explode; implode returns a string representation of a list
  3.   object."
  4.   (coerce list 'string))
  5. (defun explode (string)
  6.   "The inverse of implode; explode returns a list representation of a string."
  7.   (coerce string 'list))
Copy Code
I wrote these for a project I was working on, they're simple wrappers but they're worth keeping around .
Mark.

TOP

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.

TOP

abstract:

(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))))


Make sure you have HyperSpec.

TOP

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:
  1. (defun roman-digit->arabic (v)
  2.   "Converts a given single roman numeral into its equivalent arabic value."
  3.   (case v
  4.     ;; Maps each character used in the roman number system onto their value.
  5.     ;; Returns the value corresponding to V or NIL otherwise.
  6.     (#\M 1000)
  7.     (#\D 500)
  8.     (#\C 100)
  9.     (#\L 50)
  10.     (#\X 10)
  11.     (#\V 5)
  12.     (#\I 1)))
  13. (defun roman->arabic-helper (rest last sum)
  14.   "A none-strict utility that converts roman numerals into equivalent arabic
  15.   numbers; works on a list of numbers :)."
  16.   (if (consp rest)
  17.     (let ((this (car rest))
  18.           (rest (cdr rest)))
  19.       ;; Recursively calls roman->arabic-helper on the rest of the list; if
  20.       ;; the last number was less than this one then subtract the new value,
  21.       ;; else add them 0_o.
  22.       (roman->arabic-helper rest this
  23.           (funcall (if (< last this) ; Return and evaluate the +/- function.
  24.                      #'-
  25.                      #'+)
  26.                    this sum)))
  27.     sum)) ;Returns sum once the whole list has been processed.
  28. (defun roman->arabic (v)
  29.   "Called to convert a roman numeral into it's fully expanded arabic form."
  30.   (roman->arabic-helper
  31.       ;; Calls roman-digit->arabic-value on each item of the newly coerced
  32.       ;; list of characters.
  33.       (mapcar #'roman-digit->arabic
  34.           (coerce v 'list))
  35.       0 0))
Copy Code
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.

TOP

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).

TOP

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.

TOP


Originally Posted by netytan
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.
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.

TOP

abstract:

(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))))


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
  1.           (funcall (if (< last this)
  2.                      #'-
  3.                      #'+)
  4.                    this sum)))
Copy Code
Code:
                                Fixed
  1.           (funcall (if (< this last)
  2.                      #'-
  3.                      #'+)
  4.                    sum this)))
Copy Code
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

TOP

Back Forum