From: ·········@math.ufl.edu
Subject: Complex numbers printing as x+yi
Date: 
Message-ID: <1189518939.152295.169960@k79g2000hse.googlegroups.com>
How can I cause CL to always print complex numbers in the
form 5+7i, rather than #C(5 7) ?

If "the complex numbers" were a struct that I had defined
with `defstruct', then I simply would have specified the

	:print-function

option appropriately.        Sincerely, -Jonathan King

--
Prof. Jonathan LF King	 Mathematics dept, Univ. of Florida
<squash[[at]]math.ufl.edu>,	 <http://www.math.ufl.edu/~squash/>

From: Willem Broekema
Subject: Re: Complex numbers printing as x+yi
Date: 
Message-ID: <1189539240.805378.103550@t8g2000prg.googlegroups.com>
On Sep 11, 3:55 pm, ·········@math.ufl.edu wrote:
> How can I cause CL to always print complex numbers in the
> form 5+7i, rather than #C(5 7) ?

Common Lisp provides a neat way to define special print functions for
certain objects, without modifying the standard pretty printer (or
other programs in the same image) by means of pretty print dispatch
tables.

First copy the standard dispatch table, and add the special print
rule:

(defvar *my-pprint-dispatch* (copy-pprint-dispatch nil))

(defun my-print-complex (stream c)
  (format stream "~S+~Si" (realpart c) (imagpart c)))

(set-pprint-dispatch 'complex #'my-print-complex 0 *my-pprint-
dispatch*)

The variable *print-pprint-dispatch* controls the dispatch table used:

cl-user(5): (let ((numbers '(1 #C(3 42) 2.4)))
              (print numbers)
              (let ((*print-pprint-dispatch* *my-pprint-dispatch*))
                (print numbers))
              (print numbers)
              (values))
(1 #C(3 42) 2.4)
(1 3+42i 2.4)          ;; different indeed
(1 #C(3 42) 2.4)
cl-user(6): (setq x #C(1 2))
#C(1 2)
cl-user(7): (setf *print-pprint-dispatch* *my-pprint-dispatch*)
 ;; override standard from now on
#S(excl::pprint-dispatch-struct ...)
cl-user(8): **
1+2i
cl-user(9):


- Willem
From: Kent M Pitman
Subject: Re: Complex numbers printing as x+yi
Date: 
Message-ID: <uodg8j56j.fsf@nhplace.com>
Willem Broekema <········@gmail.com> writes:

> On Sep 11, 3:55 pm, ·········@math.ufl.edu wrote:
> > How can I cause CL to always print complex numbers in the
> > form 5+7i, rather than #C(5 7) ?
> 
> Common Lisp provides a neat way to define special print functions for
> certain objects, without modifying the standard pretty printer (or
> other programs in the same image) by means of pretty print dispatch
> tables.

Of course, if you do this, you break read/print invertibility properties.

I believe you want to either make your handler sensitive to at least
*print-readably* if not also *print-escape* so that you don't break
other code that uses the printer in ways that may not be apparent
(such as printing to a string).

You might also want to make a *pprint-pretty-complex* which can be set
to NIL regardless of such settings in order to disable the feature either
globally or in a dynamic scope without undefining/uninstalling things.

Or, if you do the approach you suggested, just skipping the last step of
installing it globally by assigning *pprint-dispatch* and instead using
a printer function you create (something slightly less lame than MY-PPRINT)
which binds *pprint-dispatch* just locally for the call would work.

But it's otherwise a nice worked example.
From: Alan Crowe
Subject: Re: Complex numbers printing as x+yi
Date: 
Message-ID: <86veagrrz1.fsf@cawtech.freeserve.co.uk>
Willem Broekema <········@gmail.com> writes:

> (defun my-print-complex (stream c)
>   (format stream "~S+~Si" (realpart c) (imagpart c)))

I doubt the original poster would be happy to see #c(3 -4)
print as 3+-4i, so one will either have to handle the sign
oneself, perhaps

(set-pprint-dispatch 'complex 
                     (lambda (stream number)
                       (if *print-readably*
                           (with-standard-io-syntax 
                             (write number :stream stream))
                           (format stream "~A~:[~;+~]~Ai"
                                   (realpart number)
                                   (not (minusp (imagpart number)))
                                   (imagpart number)))))

or use ·@F to require a leading +

(set-pprint-dispatch 'complex 
                     (lambda (stream number)
                       (if *print-readably*
                           (with-standard-io-syntax 
                             (write number :stream stream))
                           (format stream ·····@5fi"
                                   (realpart number)
                                   (imagpart number)))))

Alan Crowe
Edinburgh
Scotland
From: ·········@math.ufl.edu
Subject: Re: Complex numbers printing as x+yi
Date: 
Message-ID: <1189635268.979678.169250@22g2000hsm.googlegroups.com>
Thank you all for the quick and informative replies.  I'll
post again after Rosh. H., but let me toss out the first
question that comes to mind.

Why are there three mechanisms for altering the printing?
E.g, if I define a struct (the quaternions, say) then I can
specify how instances print, via

	:print-function   (in the `defstruct')

or the

	(defmethod print-object  .... )

approach, or by means of the

	(set-pprint-dispatch ... )

technique.

  Who overrides whom?  More importantly for me, what are
the distinctions between the *intended uses* of the three methods?

I can see that  :print-function gives good "locality of
reference", since it is actually *in* the defstruct.
OTOHand, the `defmethod' could be put right after the
defstruct.  As for the dispatch table, the  user can make a
private copy and then write to his private copy, modifying
for type 'quaternion right next to where he defstruct'ed
the quaternions.    So the locality of reference of the
other two techniques isn't bad.

   Sincerely, -Jonathan
--
Prof. Jonathan LF King	 Mathematics dept, Univ. of Florida
From: Barry Margolin
Subject: Re: Complex numbers printing as x+yi
Date: 
Message-ID: <barmar-8BAC3E.21442112092007@comcast.dca.giganews.com>
In article <························@22g2000hsm.googlegroups.com>,
 ·········@math.ufl.edu wrote:

> Thank you all for the quick and informative replies.  I'll
> post again after Rosh. H., but let me toss out the first
> question that comes to mind.
> 
> Why are there three mechanisms for altering the printing?
> E.g, if I define a struct (the quaternions, say) then I can
> specify how instances print, via
> 
> 	:print-function   (in the `defstruct')

For compatibility with pre-ANSI Common Lisp, which didn't have CLOS.

> or the
> 
> 	(defmethod print-object  .... )

When CLOS was added to the language, this more general approach was 
added.  But we didn't want to break old code that uses :PRINT-FUNCTION.

> 
> approach, or by means of the
> 
> 	(set-pprint-dispatch ... )
> 
> technique.
> 
>   Who overrides whom?  More importantly for me, what are
> the distinctions between the *intended uses* of the three methods?

SET-PPRINT-DISPATCH is only used when pretty-printing, and takes 
precedence in that case.

:PRINT-FUNCTION and :PRINT-OBJECT are equivalent to defining a 
PRINT-OBJECT method, so they have the same precedence.  Whichever one 
has been done most recently will be in effect.

-- 
Barry Margolin, ······@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
From: Marco Antoniotti
Subject: Re: Complex numbers printing as x+yi
Date: 
Message-ID: <1189526839.111284.160900@r29g2000hsg.googlegroups.com>
(defmethod print-object ((c complex) (s stream))
    (format s "~S+~Si" (realpart c) (imagpart c)))

will work (the CL environment may complain in various ways; LW
eventually lets you do that).  But then you would not be able to read
back in the number.  You loose the "what you read is what you print
and viceversa" quality of CL.

In other words, you are advised not to do that.

Cheers

Marco



On Sep 11, 9:55 am, ·········@math.ufl.edu wrote:
> How can I cause CL to always print complex numbers in the
> form 5+7i, rather than #C(5 7) ?
>
> If "the complex numbers" were a struct that I had defined
> with `defstruct', then I simply would have specified the
>
>         :print-function
>
> option appropriately.        Sincerely, -Jonathan King
>
> --
> Prof. Jonathan LF King   Mathematics dept, Univ. of Florida
> <squash[[at]]math.ufl.edu>,        <http://www.math.ufl.edu/~squash/>