; First 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) (element-sum (prefix-values-removed (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 result of removing prefixes from number-list. ; Number-list is assumed to contain only numbers. ; A prefix is a number that is less than its successor in the list. ; The prefix and its successor are replaced by the difference between ; the successor value and the prefix. ; Sample call: (prefix-values-removed '(10 1 5)), which should return ; (10 4). (define (prefix-values-removed number-list) (cond ((null? number-list) '( )) ((null? (cdr number-list)) number-list) ((and (null? (cddr number-list)) ; length = 2? (>= (car number-list) (cadr number-list)) ) number-list) ((not (starts-with-prefix? number-list)) (cons (car number-list) (prefix-values-removed (cdr number-list)) ) ) ((starts-with-prefix? number-list) (cons (- (car (prefix-values-removed (cdr number-list))) (car number-list) ) (cdr (prefix-values-removed (cdr 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)) ) ; Return the sum of the values in number-list. ; Number-list is assumed to contain only numbers. (define (element-sum number-list) (if (null? number-list) 0 (+ (car number-list) (element-sum (cdr number-list)) ) ) )