[Openmcl-devel] square bracket list
Pascal J. Bourguignon
pjb at informatimago.com
Sun Jul 24 12:29:43 PDT 2011
Joshua TAYLOR <joshuaaaron at gmail.com> writes:
> On Sun, Jul 24, 2011 at 6:07 AM, Taoufik Dachraoui
> <dachraoui.taoufik at gmail.com> wrote:
>> to be more precise, is there a way to print lists depending on the car
>> of the list
>> (cond
>> ((eq (car lst) #\[) (print list using [...]))
>> ((eq (car lst) #\{) (print list using {...}))
>> (t (print list using (...))))
>
> Pascal already responded with a solution that you said works for you.
> Here's one that's a bit shorter, and uses the lisp pretty printer.
> Pretty print dispatch tables dispatch to a printing function based on
> matching type specifiers to objects, and fortunately there's a (cons
> car-type cdr-type) type specifier, and an (eql X) type specifier, so
> your special lists can be matched. E.g.,
>
> CL-USER > (typep '(#\[ 1 2 3) '(cons (eql #\[) t))
> T
>
> So, here's a solution using that technique:
>
>
>
> (defparameter *list-pprint-dispatch*
> (copy-pprint-dispatch))
>
> (defun special-list-printer (left right)
> #'(lambda (stream list)
> (pprint-logical-block (stream list :prefix left :suffix right)
> (pprint-linear stream (rest list) nil))))
>
> (set-pprint-dispatch '(cons (eql #\[) t)
> (special-list-printer "[" "]")
> 0 *list-pprint-dispatch*)
>
> (set-pprint-dispatch '(cons (eql #\{) t)
> (special-list-printer "{" "}")
> 0 *list-pprint-dispatch*)
>
> (defun special-print (object)
> (write object
> :pprint-dispatch *list-pprint-dispatch*
> :pretty t))
>
That's great. A nice example of the power of the pretty printer.
--
__Pascal Bourguignon__ http://www.informatimago.com/
A bad day in () is better than a good day in {}.
More information about the Openmcl-devel
mailing list