[ create a new paste ] login | about

Link: http://codepad.org/DOBTZzzd    [ raw code | output | fork ]

programmingpraxis - Scheme, pasted on Jul 24:
; number words

(define (digits n . args)
  (let ((b (if (null? args) 10 (car args))))
    (let loop ((n n) (d '()))
      (if (zero? n) d
          (loop (quotient n b)
                (cons (modulo n b) d))))))

(define (letter n)
  (string-ref "ABCDEFGHIJKLMNOPQRSTUVWXYZ" (- n 1)))

(define (word cs ds)
  (if (null? ds) (list cs)
    (if (null? (cdr ds)) (list (cons (letter (car ds)) cs))
      (if (zero? (car ds)) (word cs (cdr ds))
        (let ((n (+ (* (car ds) 10) (cadr ds))))
          (if (< 26 n)
              (word (cons (letter (car ds)) cs) (cdr ds))
              (append (word (cons (letter (car ds)) cs) (cdr ds))
                      (word (cons (letter n) cs) (cddr ds)))))))))

(define (words n)
  (map list->string (map reverse (word (list) (digits n)))))

(display (words 1234))


Output:
1
(ABCD AWD LCD)


Create a new paste based on this one


Comments: