String format does not work

#lang racket
(printf "width1: |~6d|~6d|\n" 12 345)

Why dose it cause error message " printf: ill-formed pattern string
explanation: tag ~6 not allowed; arguments were: 12 345"?

Ps: on Drracket v8.17

~ is a directive, followed by a letter:

[

13.5 Writing
docs.racket-lang.org

](https://docs.racket-lang.org/reference/Writing.html#%28def._%28%28quote._~23~25kernel%29._fprintf%29%29)

If you want a tilde, use ~~.

Hi & welcome!

Are you trying to format the numbers to 6 digits?

Perhaps you were after something like this:

#lang racket
(define a (~r 12 #:min-width 6))
(define b (~r 345 #:min-width 6))
(printf "width1: ~a~a\n" a b)

which prints

width1:     12   345

~r is documented in 4.4 Strings

2 Likes

Many thanks!

2 Likes

There are alternative format implementations available that do support widths and other features not in the bare-bones one built in to Racket.

SRFI-48 Intermediate Format Strings (Comes bundled with racket):

> (require srfi/48)
> (format #t "width1: |~6F|~6F|\n" 12 345) ; note F instead of d
width1: |    12|   345|

or my slib-format:

> (require slib/format)
> (printf "width1: |~6d|~6d|\n" 12 345)
width1: |    12|   345|
2 Likes

How can I know if the function 'format' comes from Racket or srfi/48?

If you hover over the identifier in DrRacket it'll tell you which module provided it.

How to return to the function 'forrmat' of Racket in the following code?

Hi,

You can use prefix-in when you want to rename an import so it doesn't overwrite and existing definition:

> (require  (prefix-in alt: srfi/48))
> (alt:format #t "width1: |~6F|~6F|\n" 12 345) 
width1: |    12|   345|
> (format "a:~a b:~a ~n" 12 345)
a:12 b:345

See 3.2 Importing and Exporting: require and provide for more details.

Rest regards,
Stephen :beetle:

2 Likes

Many thanks for these good info

1 Like