Created
January 29, 2016 23:37
-
-
Save tormaroe/90ddd9dc7cc191040be4 to your computer and use it in GitHub Desktop.
Add roman numerals in Common Lisp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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