Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;;; One-liner
- (defun roman-to-dec (string)(let*((dict(pairlis '(#\I #\V #\X #\L #\C #\D #\M) '(1 5 10 50 100 500 1000)))(lookup(lambda(c)(cdr(assoc c dict)))))(reduce(lambda(x y)(let((l(funcall lookup y)))(- (+ x l)(* 2(rem x l)))))(coerce string 'list):initial-value 0)))
- ;;; Formatted version
- (defun roman-to-dec(string)
- (let* ((dict (pairlis '(#\I #\V #\X #\L #\C #\D #\M)
- '(1 5 10 50 100 500 1000)))
- (lookup (lambda (c) (cdr (assoc c dict)))))
- (reduce (lambda (x y) (let ((l (funcall lookup y)))
- (- (+ x l) (* 2 (rem x l)))))
- (coerce string 'list)
- :initial-value 0)))
Add Comment
Please, Sign In to add comment