Skip to content

Instantly share code, notes, and snippets.

@tormaroe
Created January 29, 2016 23:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tormaroe/90ddd9dc7cc191040be4 to your computer and use it in GitHub Desktop.
Save tormaroe/90ddd9dc7cc191040be4 to your computer and use it in GitHub Desktop.
Add roman numerals in Common Lisp
(defun mapcn (chars nums string)
(loop as char across string
as i = (position char chars)
collect (and i (nth i nums))))
(defun parse-roman (R)
(loop with nums = (mapcn "IVXLCDM" '(1 5 10 50 100 500 1000) R)
as (A B) on nums if A sum (if (and B (< A B)) (- A) A)))
(defun +roman (&rest rx)
(format nil "~@R"
(reduce #'+ (mapcar #'parse-roman rx))))
;;
(+roman "I" "III" "M") ;==> "MIV"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment