SketchyLISP Reference |
Copyright (C) 2006 Nils M Holm |
<<[null?] | [Index] | [odd?]>> |
Conformance: R5RS (Restrictions: Accepts only strings representing integers. )
Purpose: Convert a number to a string. The radix must be in the range 2..16. If no radix is given, 10 is assumed.
Arguments:
N - integer
RADIX - optional radix
Implementation:
(define (number->string n . radix) (letrec ((digits (string->list "0123456789abcdef")) (conv (lambda (n rdx res) (cond ((zero? n) res) (#t (conv (quotient n rdx) rdx (cons (list-ref digits (remainder n rdx)) res)))))) (get-radix (lambda () (cond ((null? radix) 10) ((< 1 (car radix) 17) (car radix)) (#t (bottom '(bad radix in number->string))))))) (cond ((zero? n) "0") ((negative? n) (list->string (cons #\- (conv (abs n) (get-radix) '())))) (#t (list->string (conv n (get-radix) '()))))))
Example:
(number->string -2748 16) => "-abc"
See also:
string->number.
<<[null?] | [Index] | [odd?]>> |