Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;; Constants
- (define MarcusAurelius (cons "I" (cons "V" (cons "X" (cons "L"
- (cons "C" (cons "D" (cons "M" empty))))))))
- (define panem_et_circenses (cons 1 (cons 5 (cons 10 (cons 50
- (cons 100 (cons 500 (cons 1000 empty))))))))
- (define (pos? input stoic counter) ;my way of thinking about recursion isn't strong, taking the easy way out
- (cond ;w/ a counter parameter. Is it really possible to do this with recursion?!
- [(= (string-length input) 0) 0]
- [(string=? (substring input 0 1) (first stoic)) counter]
- [else (pos? input (rest stoic) (add1 counter))]))
- (define (list-ref?! Arabic pos)
- (cond
- [(empty? Arabic) false]
- [(= pos 0) (first Arabic)]
- [else (list-ref?! (rest Arabic) (- pos 1))]))
- (define (roman->number rnum)
- (cond
- [(= (string-length rnum) 0) 0]
- [(= (string-length rnum) 1) (list-ref?! panem_et_circenses (pos? rnum MarcusAurelius 0))]
- [else
- (cond
- [(and (< (pos? (substring rnum 0 1) MarcusAurelius 0) (pos? (substring rnum 1 2) MarcusAurelius 0)) ;IV IX XL XC CD CM
- (or (and (= 0 (pos? (substring rnum 0 1) MarcusAurelius 0))
- (or (= 1 (pos? (substring rnum 1 2) MarcusAurelius 0)) (= 2 (pos? (substring rnum 1 2) MarcusAurelius 0))))
- (and (= 2 (pos? (substring rnum 0 1) MarcusAurelius 0))
- (or (= 3 (pos? (substring rnum 1 2) MarcusAurelius 0)) (= 4 (pos? (substring rnum 1 2) MarcusAurelius 0))))
- (and (= 4 (pos? (substring rnum 0 1) MarcusAurelius 0))
- (or (= 5 (pos? (substring rnum 1 2) MarcusAurelius 0)) (= 6 (pos? (substring rnum 1 2) MarcusAurelius 0))))))
- (+ (- (list-ref?! panem_et_circenses (pos? (substring rnum 1 2) MarcusAurelius 0))
- (list-ref?! panem_et_circenses (pos? (substring rnum 0 1) MarcusAurelius 0)))
- (roman->number (substring rnum 2)))]
- [else (+ (list-ref?! panem_et_circenses (pos? (substring rnum 0 1) MarcusAurelius 0)) (roman->number (substring rnum 1)))])]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement