[racket-dev] racket/format

From: Ryan Culpepper (ryan at cs.utah.edu)
Date: Fri Sep 7 20:31:06 EDT 2012

The change to ~a, ~v, and ~s seems reasonable to me. The renamed 
keywords and generalization of ~v and ~s to multiple arguments are good.

The ~r function seems okay except for the way it chooses which notation 
(positional or exponential) to use. It should take a rule, not an 
answer, and the default rule should not depend on the precision 
argument, as it produces peculiar results. Contrast:

   (catn pi #:precision 0) = "3"
   (~r pi #:precision 0) = "3e+00"

Unfortunately, I think the kind of rules supported by 'catn' 
(pos/exp-range) are also wrong. Probably the best solution is to take a 
function. I'll change ~r to drop the #:exponential? argument and instead 
take a #:notation argument, with contract

   (or/c 'positional 'exponential
         (-> rational? (or/c 'positional 'exponential)))

and default value of 'positional.

Ryan


On 09/07/2012 10:27 AM, Matthew Flatt wrote:
> I've added a `racket/format' library that is a revised version of
> Ryan's `unstable/cat'. It's re-exported by `racket', but not by
> `racket/base'.
>
> I think Ryan's library is a step in the right direction for string
> formatting, and the difficult remaining task is getting the names
> right. So, this `racket/format' experiment changes all of the names.
>
> As reflected in the `racket/format' library name, the new function
> names are intended to leverage your experience with `format': the `cat'
> function is `~a', the `catp' function is `~v', and the `catw' function
> is `~s'. The `catn' functions are collapsed to a single `~r'.
>
> Using this library, instead of writing
>
>    (format "The result of\n  ~s\nis\n  ~v"
> 	  expr
>            (eval expr))
>
> you'd write
>
>    (~a "The result of\n"
>        "  " (~s expr) "\n"
>        "is\n"
>        "  " (~v (eval expr)))
>
> or even, when using `#lang at-exp racket',
>
>    @~a{The result of
>          @~s[expr]
>        is
>          @~v[(eval expr)]}
>
> Keyword options like `#:width' and `#:align', of course, can be an even
> bigger help for formatting.
>
> I concede that names like `~s' are not remotely mnemonic unless you've
> already learned the somewhat obscure `format' escapes, but it seems
> difficult to find compact names that are meaningful and that don't
> cause collisions; we can at least take advantage of the compact names
> that we've already learned. Embracing tildes also means that we worry
> less about replacing all sorts of APIs, such as `error', that have
> `format' built in. And maybe `~a' works relatively well as a shorthand
> for `string-append'.
>
> _________________________
>    Racket Developers list:
>    http://lists.racket-lang.org/dev
>


Posted on the dev mailing list.