[pro] Phylosophical question on "syntactic" constructors

Pascal J. Bourguignon pjb at informatimago.com
Wed Dec 5 07:01:51 UTC 2012


Marco Antoniotti
<antoniotti.marco-Dh89VHlp6JQSLDncKRkoXw at public.gmane.org> writes:

> Dear all,
>
> I was fooling around with reader macros to implement some - let's say
> - "tuple" syntax.
>
> Now, I am just curious about the general thinking on this.
>
> First of all, let's note that the problem here in the conflating of
> the "constructor" with the "printed representation" of an item.
>  I.e., in Matlab you say
>
>>> [1, 2, 40 + 2]
> ans =
> 1 2 42
>
>
> In CL, doing the simple thing, you get
>
> cl-prompt> [1 2 (+ 40 2)]
> [1 2 42]
>
> but
>
> cl-prompt> #(1 2 (+ 40 2))
> #(1 2 (+ 40 2))
>
> So, suppose you have your [ … ] reader macro, would you have it work
> "functionally" or "quoting-ly" (for want of a better word)?  I am
> curious.  Note that the Matlab-style version would not break
> referential transparency if you did not have mutations.


The question is whether this is a syntax that is printable readably or
not.

As you constated, [1 2 (+ 40 2)] prints as [1 2 42] and  [1 2 42] reads
as  [1 2 42], so far so good. 

How does [1 2 '(+ 40 2)] print?  If it prints with *print-readably*
bound to t as  [1 2 '(+ 40 2)]  then it's ok.  If it prints as  
[1 2 (+ 40 2)]  then you didn't respect the *print-readably* condition.

The data structure built when reading [1 2 '(+ 40 2)] could also be
printed quite differently depending on *print-readably*.

With *print-readably* = t   ['(+ 40 2)] could print as #((+ 40 2))
With *print-readably* = nil ['(+ 40 2)] could print as [(+ 40 2)]
(or some other alternative).


Finally, it's not so hard to introduce the needed quotes when printing
readably a readable form that works "functionally".


IMO, the printable readably property is a good thing and should be
provided as much as possible.  If your tuples are not just lists or
vectors, then you can provide a print-object method to do it nicely:

cl-user> (setf *print-readably* t)
T

cl-user> (let ((x 33)) [1 2 (+ 40 2) x :x])
[1 2 42 33 :X]

cl-user> [1 2 42 33 :X]
[1 2 42 33 :X]

cl-user> [1 2 '(+ 40 2) 'x ':x]
[1 2 '(+ 40 2) 'X :X]

cl-user> [1 2 '(+ 40 2) 'X :X]
[1 2 '(+ 40 2) 'X :X]

cl-user> (setf *print-readably* nil)
NIL

cl-user> [1 2 '(+ 40 2) 'X :X]
#<TUPLE (1 2 '(+ 40 2) 'X :X) #xB16B00B5> ; but it could also print readably 



-- 
__Pascal Bourguignon__                     http://www.informatimago.com/
A bad day in () is better than a good day in {}.




More information about the pro mailing list