Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Rosetta PGA-TRAM

by Arunbear (Parson)
on Feb 11, 2011 at 17:46 UTC ( #887659=note: print w/ replies, xml ) Need Help??


in reply to Rosetta PGA-TRAM

This is an implementation with lots of parentheses :)

#lang scheme (define (sum-of-ints li [sum 0]) (if [< (length li) 2] (+ sum (apply + li)) (let ([f (first li)] [s (second li)]) (if [> s f] (sum-of-ints (cddr li) (+ sum (- s f))) (sum-of-ints (cdr li) (+ sum f)))))) (define roman-to-dec '()) (set! roman-to-dec (let ([rtoa #hash((#\M . 1000) (#\D . 500) (#\C . 100) (#\L . 50) (#\X . 10) (#\V . 5) (#\I . 1))]) (lambda (roman) (sum-of-ints (map (lambda (x) (hash-ref rtoa x)) (string->list (string-upcase roman))))))) (define test-data '("XLII" "LXIX" "mi" "MCMLXXXV")) (for ([r test-data]) (printf "~a ~a~n" r (roman-to-dec r)))
+% mzscheme roman.ss XLII 42 LXIX 69 mi 1001 MCMLXXXV 1985
Like the perl version, it uses a lexical scope to hide the hash.

It however uses a method of combining the numbers that is closer to how we'd do it in our heads i.e. following the Subtractive principle


Comment on Re: Rosetta PGA-TRAM
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://887659]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (10)
As of 2015-07-29 16:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (265 votes), past polls