[plt-scheme] problem with string

From: Arjun Guha (GUHAARJU at grinnell.edu)
Date: Sun Jan 9 12:08:31 EST 2005

Let's look at each character in your string:

> (string-ref (str$ 176 5) 0)
#\nul
> (string-ref (str$ 176 5) 1)
#\nul
> (string-ref (str$ 176 5) 2)
#\1
> (string-ref (str$ 176 5) 3)
#\7
> (string-ref (str$ 176 5) 4)
#\6

You are padding with ascii character 0 (#\nul or \u0000), not the characer #\0 
(zero).

You need not have an explicit loop.  I believe the following procedure is 
equivalent to yours:

> (define (str$ val exp)
>   (let* ((val-str (number->string val))
>          (val-str-length (string-length val-str)))
>     (string-append (make-string (- exp val-str-length) #\0) val-str)))

-Arjun

--
Arjun Guha <guhaarju at grinnell.edu>



Posted on the users mailing list.