|
Prev: changing file type
Next: inherited slot types (cmucl)
From: John Thingstad on 17 Jun 2008 15:28 Just read through the document "XP A Common Lisp Pretty Printing system" (http://dspace.mit.edu/handle/1721.1/6503) Wrote this trivial test: Given a the factulty of 100 print the digits in groups of 3 seperated by space so that they fill the line. If the number sequence is less than 3 digits like 10! = 3628800 print it "003 628 300" (defpackage :my-user (:use :cl :iterate :split-sequence)) (in-package :my-user) (defun fact (n) (check-type n (integer 0 *)) (if (< n 2) 1 (iter (for i from 2 to n) (multiplying i)))) (defun print-fact (n) (let* ((fact (fact n)) (fact-string (format nil "~:D" fact)) (fact-list (split-sequence #\, fact-string))) (format t "~&~<~{~3,,,'0@A~^~4T~:_~}~:>" (list fact-list)) (values))) Ok so compute fact 10! = 3628300 print it with , separator 362800 = "3,628,300" split it on , to a list ("3" "628" "300") Now ~< ... ~:> sais to use the pretty printer (pprint-logical-block). It opens the list but provides no way to iterate through it. Thus (list fact-list) is needed to give ~{...~^...~} something to work on. (iterate through a list using the bit after ~^ as seperator) ~3,,,'0@A sais: 3 - width 3, '0 - fill empty places with 0, @ - right allign, princ it ~4T - tab to next width is 4 ~:_ - fill to end of line but break on whole triplet MY-USER 49 > (print-fact 10) 003 628 800 MY-USER 54 > (print-fact 100) 093 326 215 443 944 152 681 699 238 856 266 700 490 715 968 264 381 621 468 592 963 895 217 599 993 229 915 608 941 463 976 156 518 286 253 697 920 827 223 758 251 185 210 916 864 000 000 000 000 000 000 000 000 Well particularly readable it isn't but quite powerful. Anyone else using the pretty printer for printing data? -------------- John Thingstad
From: Rob Warnock on 18 Jun 2008 00:58 John Thingstad <jpthing(a)online.no> wrote: +--------------- | Just read through the document "XP A Common Lisp Pretty Printing system" | (http://dspace.mit.edu/handle/1721.1/6503) | Wrote this trivial test: | ... | MY-USER 54 > (print-fact 100) | 093 326 215 443 944 152 681 699 238 856 266 700 490 715 968 264 381 621 | 468 592 963 895 217 599 993 229 915 608 | 941 463 976 156 518 286 253 697 920 827 223 758 251 185 210 916 864 000 | 000 000 000 000 000 000 000 | | Well particularly readable it isn't but quite powerful. +--------------- Cool! Hmmm... Some manipulation of *PRINT-RIGHT-MARGIN* [also see *PRINT-MISER-WIDTH* and/or *PRINT-LENGTH*, in some cases] might be able to get rid of those annoying short wraps: > (let ((*print-right-margin* 40)) ; try for 10 groups per line (print-fact 100)) 093 326 215 443 944 152 681 699 238 856 266 700 490 715 968 264 381 621 468 592 963 895 217 599 993 229 915 608 941 463 976 156 518 286 253 697 920 827 223 758 251 185 210 916 864 000 000 000 000 000 000 000 000 > +--------------- | Anyone else using the pretty printer for printing data? +--------------- I tend to use it a lot for generating C source files, e.g., this bit clipped from a previous article of mine <news:F-udnZ3s2r7vWELbnZ2dnUVZ_jydnZ2d(a)speakeasy.net> [Sept'07]: I use it [the |0X| printing function shown earlier] a lot when building data initialization tables in C code: > (let ((data (loop for i below 24 nconc (list (random 0x100000000) (random 256)))) (instance "georgey")) (format t "~%foo_t ~a_foos[~d] = {~ ~%~{~<~%~1,68:; {~/0x/, ~2/0x/}~>~^,~}~%};~%" instance (/ (length data) 2) data)) foo_t georgey_foos[24] = { {0x21a41a5c, 0x87}, {0x1c63b86e, 0xb4}, {0x894c25d5, 0xa1}, {0x9979b7fe, 0xbb}, {0xc2ad44aa, 0x4d}, {0xe2826239, 0x70}, {0x053b537e, 0x05}, {0x6ac226e8, 0xbe}, {0x1252ea73, 0x20}, {0xe3001d4a, 0x12}, {0x9a006313, 0x31}, {0x299d2f64, 0x54}, {0x90feb745, 0xda}, {0xc7ed257b, 0xc1}, {0xa6e8e18a, 0x51}, {0x0fdb8569, 0xed}, {0x713c27e0, 0xa8}, {0xd975dbac, 0x2d}, {0xb4263772, 0x85}, {0xe6cdaaa9, 0x48}, {0x7db24d29, 0xf8}, {0x87e5aa36, 0xa3}, {0xb56e3dd7, 0xe2}, {0x3cf23443, 0x4e} }; NIL > -Rob ----- Rob Warnock <rpw3(a)rpw3.org> 627 26th Avenue <URL:http://rpw3.org/> San Mateo, CA 94403 (650)572-2607
From: Marco Antoniotti on 18 Jun 2008 06:15 http://www.merl.com/papers/TR93-17/ is always a good start when talking about the Pretty Printer. :) Cheers -- Marco On Jun 18, 6:58 am, r...(a)rpw3.org (Rob Warnock) wrote: > John Thingstad <jpth...(a)online.no> wrote: > > +--------------- > | Just read through the document "XP A Common Lisp Pretty Printing system" > | (http://dspace.mit.edu/handle/1721.1/6503) > | Wrote this trivial test: > | ... > | MY-USER 54 > (print-fact 100) > | 093 326 215 443 944 152 681 699 238 856 266 700 490 715 968 264 381 621 > | 468 592 963 895 217 599 993 229 915 608 > | 941 463 976 156 518 286 253 697 920 827 223 758 251 185 210 916 864 000 > | 000 000 000 000 000 000 000 > | > | Well particularly readable it isn't but quite powerful. > +--------------- > > Cool! > > Hmmm... Some manipulation of *PRINT-RIGHT-MARGIN* [also see > *PRINT-MISER-WIDTH* and/or *PRINT-LENGTH*, in some cases] > might be able to get rid of those annoying short wraps: > > > (let ((*print-right-margin* 40)) ; try for 10 groups per line > (print-fact 100)) > > 093 326 215 443 944 152 681 699 238 856 > 266 700 490 715 968 264 381 621 468 592 > 963 895 217 599 993 229 915 608 941 463 > 976 156 518 286 253 697 920 827 223 758 > 251 185 210 916 864 000 000 000 000 000 > 000 000 000 > > > > +--------------- > | Anyone else using the pretty printer for printing data? > +--------------- > > I tend to use it a lot for generating C source files, > e.g., this bit clipped from a previous article of mine > <news:F-udnZ3s2r7vWELbnZ2dnUVZ_jydnZ2d(a)speakeasy.net> [Sept'07]: > > I use it [the |0X| printing function shown earlier] > a lot when building data initialization tables in C code: > > > (let ((data (loop for i below 24 nconc (list (random 0x100000000) > (random 256)))) > (instance "georgey")) > (format t "~%foo_t ~a_foos[~d] = {~ > ~%~{~<~%~1,68:; {~/0x/, ~2/0x/}~>~^,~}~%};~%" > instance (/ (length data) 2) data)) > > foo_t georgey_foos[24] = { > {0x21a41a5c, 0x87}, {0x1c63b86e, 0xb4}, {0x894c25d5, 0xa1}, > {0x9979b7fe, 0xbb}, {0xc2ad44aa, 0x4d}, {0xe2826239, 0x70}, > {0x053b537e, 0x05}, {0x6ac226e8, 0xbe}, {0x1252ea73, 0x20}, > {0xe3001d4a, 0x12}, {0x9a006313, 0x31}, {0x299d2f64, 0x54}, > {0x90feb745, 0xda}, {0xc7ed257b, 0xc1}, {0xa6e8e18a, 0x51}, > {0x0fdb8569, 0xed}, {0x713c27e0, 0xa8}, {0xd975dbac, 0x2d}, > {0xb4263772, 0x85}, {0xe6cdaaa9, 0x48}, {0x7db24d29, 0xf8}, > {0x87e5aa36, 0xa3}, {0xb56e3dd7, 0xe2}, {0x3cf23443, 0x4e} > }; > NIL > > > > -Rob > > ----- > Rob Warnock <r...(a)rpw3.org> > 627 26th Avenue <URL:http://rpw3.org/> > San Mateo, CA 94403 (650)572-2607
From: Rob Warnock on 18 Jun 2008 07:38 Marco Antoniotti <marcoxa(a)gmail.com> wrote: +--------------- | > John Thingstad <jpth...(a)online.no> wrote: | > +--------------- | > | Just read through the document "XP A Common Lisp Pretty Printing system" | > | (http://dspace.mit.edu/handle/1721.1/6503) .... | http://www.merl.com/papers/TR93-17/ | | is always a good start when talking about the Pretty Printer. :) +--------------- To start with, yes, but there's only about 7 pages on the Pretty Printer in TR93-17, whereas the one John quoted [which is really AIM-1102, after you drill down through the DSpace re-org of the AI docs!] has the full 46-page original "XP" paper. -Rob p.s. Of course, TR93-17 *also* has the MACROEXPAND-ALL tutorial... ----- Rob Warnock <rpw3(a)rpw3.org> 627 26th Avenue <URL:http://rpw3.org/> San Mateo, CA 94403 (650)572-2607
From: John Thingstad on 18 Jun 2008 08:54
P� Wed, 18 Jun 2008 13:38:12 +0200, skrev Rob Warnock <rpw3(a)rpw3.org>: > Marco Antoniotti <marcoxa(a)gmail.com> wrote: > +--------------- > | > John Thingstad <jpth...(a)online.no> wrote: > | > +--------------- > | > | Just read through the document "XP A Common Lisp Pretty Printing > system" > | > | (http://dspace.mit.edu/handle/1721.1/6503) > ... > | http://www.merl.com/papers/TR93-17/ > | > | is always a good start when talking about the Pretty Printer. :) > +--------------- > > To start with, yes, but there's only about 7 pages on the Pretty Printer > in TR93-17, whereas the one John quoted [which is really AIM-1102, > after you drill down through the DSpace re-org of the AI docs!] has > the full 46-page original "XP" paper. > OPPs just noticed this is AIM-1102 and I was reading AIM-1102A that came out a half year later and contains modifications made by demand of the ANSI committee. http://dspace.mit.edu/handle/1721.1/6504 I suggest reading that instead because 1. the #" reader macro isn't defined in CL 2. define-print-dispatch is replaced by set-pprint-dispatch -------------- John Thingstad |