[racket] Is there anything like the cl format?

조회수 121회
읽지 않은 첫 메시지로 건너뛰기

Manfred Lotz

읽지 않음,
2010. 12. 17. 오전 1:58:2810. 12. 17.
받는사람 us...@racket-lang.org
Hi there,
Common Lisp has format to print stuff like for example:

> (format t "~,5f" pi)
3.14159


I found format and printf but they seem to be a bit poor in term of
printing capabilities.

Is there anything similar to CL's format in Racket?


--
Manfred


_________________________________________________
For list-related administrative tasks:
http://lists.racket-lang.org/listinfo/users

Neil Van Dyke

읽지 않음,
2010. 12. 17. 오전 2:33:0010. 12. 17.
받는사람 Manfred Lotz, us...@racket-lang.org
Manfred Lotz wrote at 12/17/2010 01:58 AM:
[...]
>> (format t "~,5f" pi)
>>
[...]

> Is there anything similar to CL's format in Racket?
>

It would be nice to have a some special support for decimal-point
formatting in Racket's "format". Just the number of decimal places to
print would be nice. (Other formatting, such as whitespace padding, is
a lot less relevant now that we use HTML and graphics languages rather
than ASCII text with fixed-pitch characters for formatting. And I
woudn't want CL's "format" -- I recall thinking it was an overpowered
minilanguage to be stuffing into a string.)

If nobody steps up to the plate for number formatting, I will make a
PLaneT package out of some code in my Racket invoicing setup. This code
defines special syntax for defining individual number formatters (for,
e.g., printing US dollars and cents in a conventional US style). It's
not the end-all-be-all, which is why I haven't PLaneT-ized it already,
but it's done what I've needed so far.

(define-syntax %make-number-displayer/macro
(syntax-rules ()
((_ sign
prefix
pad-whole-char
pad-whole-length
whole-spacers-char
whole-spacers-interval
decimal-point
max-fractional-length
pad-fractional-char
pad-fractional-length
suffix)

--
http://www.neilvandyke.org/

Manfred Lotz

읽지 않음,
2010. 12. 17. 오전 3:21:4310. 12. 17.
받는사람 us...@racket-lang.org
Hi Neil,

On Fri, 17 Dec 2010 02:33:00 -0500
Neil Van Dyke <ne...@neilvandyke.org> wrote:

> Manfred Lotz wrote at 12/17/2010 01:58 AM:
> [...]
> >> (format t "~,5f" pi)
> >>
> [...]
> > Is there anything similar to CL's format in Racket?
> >
>
> It would be nice to have a some special support for decimal-point
> formatting in Racket's "format". Just the number of decimal places
> to print would be nice. (Other formatting, such as whitespace
> padding, is a lot less relevant now that we use HTML and graphics
> languages rather than ASCII text with fixed-pitch characters for
> formatting. And I woudn't want CL's "format" -- I recall thinking it
> was an overpowered minilanguage to be stuffing into a string.)


I agree thas cl's format is a swiss knife. Not all of its features is
really required. However, having support for a certain subset of cl'
format features would be nice. The syntax should be either like format
using tilde specifiers or like C's printf using percent specifiers.


>
> If nobody steps up to the plate for number formatting, I will make a
> PLaneT package out of some code in my Racket invoicing setup. This
> code defines special syntax for defining individual number formatters
> (for, e.g., printing US dollars and cents in a conventional US
> style). It's not the end-all-be-all, which is why I haven't
> PLaneT-ized it already, but it's done what I've needed so far.
>

Thanks, that would be great.

--
Manfred

Laurent

읽지 않음,
2010. 12. 17. 오전 5:09:0410. 12. 17.
받는사람 Manfred Lotz, us...@racket-lang.org
You probably want `real->decimal-string'

Maybe it would be a good idea in the `format' page to add a pointer to that function?

Laurent

Manfred Lotz

읽지 않음,
2010. 12. 17. 오전 6:30:0210. 12. 17.
받는사람 us...@racket-lang.org
On Fri, 17 Dec 2010 11:09:04 +0100
Laurent <laurent...@gmail.com> wrote:

> You probably want
> `real->decimal-string<http://docs.racket-lang.org/reference/generic-numbers.html?q=real&q=number#%28def._%28%28lib._racket/private/base..rkt%29._real-%7E3edecimal-string%29%29>


> '
>
> Maybe it would be a good idea in the `format' page to add a pointer
> to that function?
>

Yeah, I used it. However, it isn't nice to code, and it is not quite
the same.

Compare

; if it were available
(printf "Total: %4.2f/%2.2f %4.2f/%2.2f\n" fnum1 fnum2 fnum3 fnum4)

to this:
(let ([pfnum1 (real->decimal-string fnum1 2)]
[pfnum2 (real->decimal-string fnum2 2)]
[pfnum3 (real->decimal-string fnum3 2)]
[pfnum4 (real->decimal-string fnum4 2)])
(printf "Total: ~a/~a ~a/~a" pfnum1 pfnum2 pfnum3 pfnum4))

Richard Cleis

읽지 않음,
2010. 12. 17. 오전 7:41:0410. 12. 17.
받는사람 Manfred Lotz, us...@racket-lang.org
Someone on this list, several years ago, wrote scheme functions that
were similar to C/printf/formats. I don't have time to look for the
files right now, but they might do everything you want.

RAC

Chongkai Zhu

읽지 않음,
2010. 12. 17. 오후 12:46:1610. 12. 17.
받는사람 Manfred Lotz, us...@racket-lang.org
There's SRFI 48 and SRFI 54, both supported in Racket:

#lang racket
(require srfi/48)
(format "~6,5F" pi)

=>

"3.14159"

-
Chongkai

On 12/17/2010 12:58 AM, Manfred Lotz wrote:
> Hi there,
> Common Lisp has format to print stuff like for example:
>
>> (format t "~,5f" pi)
> 3.14159
>
>
> I found format and printf but they seem to be a bit poor in term of
> printing capabilities.
>
> Is there anything similar to CL's format in Racket?
>
>
>
>

_________________________________________________

Manfred Lotz

읽지 않음,
2010. 12. 17. 오후 2:00:3210. 12. 17.
받는사람 us...@racket-lang.org
On Fri, 17 Dec 2010 11:46:16 -0600
Chongkai Zhu <cz...@cs.utah.edu> wrote:

> There's SRFI 48 and SRFI 54, both supported in Racket:
>
> #lang racket
> (require srfi/48)
> (format "~6,5F" pi)
>
> =>
>
> "3.14159"
>
> -

Great! srfi 48 is far easier to use than srfi 54 and although it seems
to be not as mighty as the format of Common Lisp it is certainly good
enough.


This I will use.


--
Thanks,
Manfred

Jos Koot

읽지 않음,
2010. 12. 17. 오후 4:01:3310. 12. 17.
받는사람 Manfred Lotz, us...@lists.racket-lang.org
Because of pressing the wrong send button, my repsonse was not transmitted
to the Racket list. Sorry.
In the meantime I agree with you to use the tools that suites you without
overpowering you desires.
Jos

> -----Original Message-----
> From: Manfred Lotz [mailto:manfre...@arcor.de]
> Sent: 17 December 2010 21:22
> To: Jos Koot
> Subject: Re: [racket] Is there anything like the cl format?
>
> On Fri, 17 Dec 2010 11:36:24 +0100
> "Jos Koot" <jos....@telefonica.net> wrote:
>
> > (require (planet "fmt.ss" ("joskoot" "planet-fmt.plt" 1
> 2))) Includes
> > integer and float formats, tabulation and much more.
> > Documentation in file fmt.doc on the planet page.
> > Jos
> >
> >
>
> Thanks Jos. Looks good.However, in the meantime I got a
> pointer to srfi
> 48 which is just what I was looking for.

Phil Bewig

읽지 않음,
2010. 12. 17. 오후 4:20:3110. 12. 17.
받는사람 Jos Koot, us...@lists.racket-lang.org
SLIB (http://people.csail.mit.edu/jaffer/SLIB.html) has format in all its CL glory.

Jakub Piotr Cłapa

읽지 않음,
2010. 12. 17. 오후 7:09:1910. 12. 17.
받는사람 Racket
Hi,

I wanted to hijack the thread to ask about one idea I implemented. I am
pretty sure this is "evil" but I do not now how to improve things.

I did a match-like macro for sync (of the event? kind). I wanted to have
short names for the clauses but I didn't really want them to "pollute"
the namespace of the using module so I did something like this (full
code is on bitbucket [1]):

(receive
[(#:after 1000) (printf "1000ms passed\n")]
[(#:recv v #:on chn) (printf "~s received from channel ~s\n" v chn)])

The important part of the implementation is:

(define-for-syntax (keyword-stx->helper stx)
(format-id stx #:source stx "receive-#:~a" (syntax-e stx)))

It takes a keyword and makes a (scoped) identifier from it by appending
receive-.

This allows the #:after and #:recv (and #:handle and #:at) clauses to be
implemented (and possibly exported from unrelated modules) like this:

(define-syntax-rule (receive-#:handle (evt-expr result) e0 e1 ...)
(handle-evt (or evt-expr never-evt)
(λ (result) (rewind!) e0 e1 ...)))

(define-syntax-rule (receive-#:at (time) e0 e1 ...)
(receive-#:handle ((let ([t time]) (and t (alarm-evt t))) _) e0 e1 ...))

(define-syntax-rule (receive-#:after (interval) e0 e1 ...)
(receive-#:at ((now+ interval)) e0 e1 ...))

(define-syntax-rule (receive-#:recv (id #:on chn) e0 e1 ...)
(receive-#:handle (chn id) e0 e1 ...))


I do not feel like this is a clean way to do this. But OTOH it kind of
respects lexical scope and module provides. Also it tries not to suprise
the developer.

The question:

Does the module system provide a way to create a completely seperate
namespace for things like this (I read the Typed Scheme macrology papers
but I failed to find a good solution to this). It should be seperate but
preferably controllable with something like provide and require.

The reason I hijacked this thread:

It would be quite nice to make a printf alternative which takes a C like
input string but converts it to a series of function/macro calls with
the names of the functions based on the letters in the original string.
Something like:

(printf "Value: %.5f from sender: % 20s\n" 5/6 robot-name)

->

(display "Value: ") (display (printf::f ".5" 5/6))
(display " from sender: ") (display (printf::s " 20" robot-name))
(display "\n")

Of course this could also be done with Scribble syntax instead of string
parsing but the main selling point are the short identifiers together
with "aftermarket" extensibility. In this case it is even more critical
not to pollute the environment with bindings for symbols like 'f, 'd or 's.

Sorry for the long post and thanks for all input on this matter.

[1]: http://bb.loee.pl/rkt-util/src/9c60a54ee7df/receive.ss

--
regards,
Jakub Piotr Cłapa

Manfred Lotz

읽지 않음,
2010. 12. 17. 오후 11:40:5710. 12. 17.
받는사람 us...@racket-lang.org
On Fri, 17 Dec 2010 15:20:31 -0600
Phil Bewig <pbe...@gmail.com> wrote:

> SLIB (http://people.csail.mit.edu/jaffer/SLIB.html) has format in all
> its CL glory.
>

Thanks, good to know although for the time being I'm happy with srfi
48's format.

전체답장
작성자에게 답글
전달
새 메시지 0개