Welcome! Please see the About page for a little more info on how this works.

+1 vote
in IO by

The documentation of cl-format claims that it is fully compatible with the CL version of format. However, in CL the ~A directive (print aesthetically) pretty-prints the corresponding argument. For example (format out "~A" some-list) outputs the same as (pprint some-list out) However, in clojure (cl-format out "~A" some-list) outputs the contents of some-list all on a single line.

Does anyone know whether it is possible to make this work correctly? I've seen the dynamic variable print-pretty which seems to have no effect on ~A.

After a bit of searching, I've found what I think is the culprit. According to line 1338 of cl_format.clj it seems ~A dispatches unconditionally to print-str rather than to some form of pprint-to-str. I suspect that is a bug, because I don't see any comment there explaining how the pretty-printer is intended to be triggered here.

It seems like it should use something like (fn [data] (with-out-str (pprint data)) rather than print-str.

1 Answer

0 votes
selected by
Best answer

I think maybe you want ~W instead? Per http://www.ai.mit.edu/projects/iiip/doc/CommonLISP/HyperSpec/Body/sec_22-3-4-3.html, "An argument, any object, is printed obeying every printer control variable (as by write). " This seems to do what you're asking for on an example (matches pprint).

The only real detailed doc for cl-format in Clojure is http://clojure.github.io/clojure/doc/clojure/pprint/CommonLispFormat.html from the author, but it has not been actively developed for a very long time (although we have done maintenance on it occasionally).

I do see a note at http://www.cs.cmu.edu/afs/cs.cmu.edu/project/ai-repository/ai/html/cltl/clm/node200.html related to ~A and ~W and pretty printing that relates to some confusion on requirements there.

Thanks.  that is really good information.   In SBCL, the CL that I use most often, ~A triggers the pretty printer.   I will investigate this further for the purpose of understanding CL better.  However, I'm quite happy to use ~W in cl-format as it seems to do exactly what I want.
BTW.  hats off and congratulations to the person who implemented cl-format for clojure.  That must have been an excruciatingly difficult project.  I for one, am grateful for the hard work.