; Revised program designed in the "Roman Numerals" case study. ; Return the decimal value of the Roman numeral whose digits are ; contained in roman-digit-list. ; Roman-digit-list is assumed to contain only Roman digits. ; Sample call: (decimal-value '(x i v)), which should return 14. (define (decimal-value roman-digit-list) (roman-sum (digit-values roman-digit-list) ) ) ; Return a list containing the decimal values of the Roman digits ; in roman-digit-list. ; Roman-digit-list is assumed to contain only Roman digits. ; Sample call: (digit-values '(x i v)), which should return (10 1 5). (define (digit-values roman-digit-list) (if (null? roman-digit-list) '( ) (cons (decimal-digit-value (car roman-digit-list)) (digit-values (cdr roman-digit-list)) ) ) ) ; Return the decimal value of the given Roman digit. (define (decimal-digit-value roman-digit) (cond ((equal? roman-digit 'm) 1000) ((equal? roman-digit 'd) 500) ((equal? roman-digit 'c) 100) ((equal? roman-digit 'l) 50) ((equal? roman-digit 'x) 10) ((equal? roman-digit 'v) 5) ((equal? roman-digit 'i) 1) ) ) ; Return the decimal value of a Roman numeral. The decimal equivalents ; of its Roman digits are contained in number-list. ; Sample call: (roman-sum '(10 1 5)), which should return 14. (define (roman-sum number-list) (cond ((null? number-list) 0) ((null? (cdr number-list)) (car number-list)) ((not (starts-with-prefix? number-list)) (+ (car number-list) (roman-sum (cdr number-list)) ) ) ((starts-with-prefix? number-list) (+ (- (cadr number-list) (car number-list)) (roman-sum (cddr number-list)) ) ) ) ) ; Return true if the number-list starts with a prefix, i.e. a number ;.that's less than the second value in the list. ; Number-list is assumed to be of length at least 2 and to contain ; only numbers. (define (starts-with-prefix? number-list) (< (car number-list) (cadr number-list)) )