#1
  1. Arcane Scribbler
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2005
    Location
    Indianapolis, IN
    Posts
    1,907
    Rep Power
    584

    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))))[/code]
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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.
    programming language development: www.netytan.com Hula

  4. #3
  5. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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.
    programming language development: www.netytan.com Hula

  6. #4
  7. Arcane Scribbler
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2005
    Location
    Indianapolis, IN
    Posts
    1,907
    Rep Power
    584
    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.
  8. #5
  9. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    Make sure you have HyperSpec.

    Comments on this post

    • netytan agrees : About to suggest the same thing Yegg
    • LyonHaert agrees : Thank you.
  10. #6
  11. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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.

    Comments on this post

    • LyonHaert agrees : Thank you, too.
    Last edited by netytan; January 25th, 2006 at 02:19 PM.
    programming language development: www.netytan.com Hula

  12. #7
  13. Arcane Scribbler
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2005
    Location
    Indianapolis, IN
    Posts
    1,907
    Rep Power
    584
    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).
  14. #8
  15. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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.
    programming language development: www.netytan.com Hula

  16. #9
  17. Arcane Scribbler
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2005
    Location
    Indianapolis, IN
    Posts
    1,907
    Rep Power
    584
    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.
  18. #10
  19. Arcane Scribbler
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2005
    Location
    Indianapolis, IN
    Posts
    1,907
    Rep Power
    584
    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]
    [code=Fixed] (funcall (if (< this last)
    #'-
    #'+)
    sum this)))[/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

    Comments on this post

    • netytan agrees : Good explanation :)
    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."

    .NET Must-Haves
    Tools: Reflector
    References: Threading in .NET
  20. #11
  21. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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.
    programming language development: www.netytan.com Hula

  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2002
    Posts
    596
    Rep Power
    23
    Originally Posted by LyonHaert
    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.
    see also Practical Common Lisp, Cliki and the CL Cookbook. On Lisp? Macro power to you!

IMN logo majestic logo threadwatch logo seochat tools logo