chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Chicken-users] Wrapping C's printf


From: Alejandro Forero Cuervo
Subject: Re: [Chicken-users] Wrapping C's printf
Date: Wed, 31 Oct 2007 23:07:03 +0100
User-agent: Mutt/1.5.16 (2007-06-11)

> A pure Scheme implementation that (IIRC) comes relatively close to
> C's printf is part of slib: [...]

Or, if only basic support for sequences such as "%d" and "%s" is
needed, this should be very easy to implement on top of format-modular:

  (use format-modular)
  (define printf-formatter
    (make-format-function #f #\%
      `(((#\D ,(formatter-padded display))
         (#\S ,(formatter-padded display))))))
  (define (printf . args) (apply printf-formatter #t args))
  (define (sprintf . args) (apply printf-formatter #f args))

You can then do:

  (printf "There are %d %s, its halloween!\n" 4 "pirates")

  (sprintf "There are %d %s, its halloween!\n" 4 "pirates")

Of course, if you actually want to support things like "%04d" with the
exactsame syntax as C's printf does it gets a little bit hairy (but
it's perfectly doable, you'll just have to copy formatter-padded and
tweak it somewhat).

Hmm, perhaps we should add printf and sprintf to format-modular and
start adding more printf-esque escape sequences?  Or perhaps make a
separate 'printf' egg with them?

Alejo.
http://azul.freaks-unidos.net/




reply via email to

[Prev in Thread] Current Thread [Next in Thread]