Subject: Re: Newby riddle
From: rpw3@rpw3.org (Rob Warnock)
Date: Thu, 23 Aug 2007 23:36:30 -0500
Newsgroups: comp.lang.lisp
Message-ID: <ooudna5HQrnT_VPbnZ2dnUVZ_gCdnZ2d@speakeasy.net>
Kyle McGivney <KyleMcG@gmail.com> wrote:
+---------------
| I like Juho's answer because it's the first time in my
| (admittedly short) time lisping I've ever seen ~/ used.
+---------------

Here's my only use of it [and also my only use of escape characters
for symbol names!]... but I use it a *LOT* in my user-mode hardware
debugger code [which also uses a ZERO-X-READER readmacro in its REPL]:

  (defun \0x (stream arg colon-p at-sign-p &optional mincol padchar)
    "Hexadecimal numeric printing for use with the FORMAT ~/.../ directive.
    Outputs ARG to STREAM as \"~(0x~mincol,padX~)\" [default \"~(0x~8,'0X~)\"].
    If COLON-P, the entire output will be capitalized instead of lowercased.
    If AT-SIGN-P is true, the \"0x\" prefix will be suppressed."
    (let* ((fmt1 "~~~:[~;:@~](~:[0x~;~]~~~:[8~;~:*~a~],'~:[0~;~:*~a~]x~~)")
	   (fmt2 (format nil fmt1 colon-p at-sign-p mincol padchar)))
      (format stream fmt2 arg)))

Examples:

    > (format t "~/0x/ == ~4/0x/ == ~2/0x/~%" #1=27 #1# #1#)
    0x0000001b == 0x001b == 0x1b
    NIL
    > (format t "~:@/0x/ == ~4:@/0x/ == ~2:@/0x/~%" #1=27 #1# #1#)
    0000001B == 001B == 1B
    NIL
    > (format t "~/0x/~%" (+ 0x1234000 27))  ; demo the readmacro
    0x0123401b
    NIL
    > 

I also use it 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
    > 

[Note how the FORMAT string carefully leaves off the comma after the
final element (even though this is no longer required in ANSI C).]


-Rob

-----
Rob Warnock			<rpw3@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607