Skip to content

Instantly share code, notes, and snippets.

@alcidesfp
Created February 22, 2012 19:40
Show Gist options
  • Save alcidesfp/1886831 to your computer and use it in GitHub Desktop.
Save alcidesfp/1886831 to your computer and use it in GitHub Desktop.
Kata Roman Numerals en Scheme Racket idiomático (funcional)
;; -*- coding: utf-8; mode: scheme -*-
;; $Id: num-roman.rkt,v 1.14 2012/02/16 04:02:35 alcides_fp Exp $
#lang racket
(provide numeral)
(define (numeral digito posicion)
(let ((matriz-numerales
#(#("" "I" "II" "III" "IV" "V" "VI" "VII" "VIII" "IX")
#("" "X" "XX" "XXX" "XL" "L" "LX" "LXX" "LXXX" "XC")
#("" "C" "CC" "CCC" "CD" "D" "DC" "DCC" "DCCC" "CM")
#("" "M" "MM" "MMM"))))
(vector-ref (vector-ref matriz-numerales posicion) digito) ) )
;;------------------------------------------------------------------------
(provide dec->list)
(define (dec->list numero)
"Obtiene lista de cifras de un número base 10 en orden inverso"
(let loop ([cantidad numero][retval '()])
(if (= cantidad 0) retval
(loop (quotient cantidad 10)
(append retval (list (remainder cantidad 10))) ) ) ) )
;;------------------------------------------------------------------------
(provide num-roman)
(define (num-roman arabigo)
(let ([cifras (dec->list arabigo)])
(for/fold ([retval ""])
(((digito posicion)(in-indexed cifras)))
(string-append (numeral digito posicion) retval) ) ) )
;;------------------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment