Skip to content

Instantly share code, notes, and snippets.

@ferd
Created August 5, 2012 16:58
Show Gist options
  • Save ferd/3265910 to your computer and use it in GitHub Desktop.
Save ferd/3265910 to your computer and use it in GitHub Desktop.
#lang racket
(require racket/match)
(require racket/string)
(define (to-roman x)
(define (digit digits)
(match digits
[(list 1 x _ _) (list x)]
[(list 2 x _ _) (list x x)]
[(list 3 x _ _) (list x x x)]
[(list 4 x y _) (list x y)]
[(list 5 _ y _) (list y)]
[(list 6 x y _) (list y x)]
[(list 7 x y _) (list y x x)]
[(list 8 x y _) (list y x x x)]
[(list 9 x _ z) (list x z)]))
(define (roman x)
(cond [(= x 0) '()]
[(>= x 1000) (cons #\M (roman (- x 1000)))]
[(>= x 100) (append (digit (list (quotient x 100) #\C #\D #\M))
(roman (remainder x 100)))]
[(>= x 10) (append (digit (list (quotient x 10) #\X #\L #\C))
(roman (remainder x 10)))]
[(>= x 1) (digit (list x #\I #\V #\X))]))
(list->string (roman x)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment