11

I'm having trouble reading output from dr racket. By default it displays lists using mcons. For example, sicp exercise 2.32 produces:

> (subsets (list 1 2 3))
(mcons
 (mcons
  '()
  (mcons
   (mcons 3 '())
   (mcons
    (mcons 2 '())
    (mcons
     (mcons 2 (mcons 3 '()))
     (mcons
      (mcons 1 '())
      (mcons
       (mcons 1 (mcons 3 '()))
       (mcons
        (mcons 1 (mcons 2 '()))
        (mcons (mcons 1 (mcons 2 (mcons 3 '()))) '()))))))))
 '())

I'm having trouble reading this. Is there a way to make the output look like:

 (() (3) (2) (2 3) (1) (1 3) (1 2) (1 2 3))

Thanks!

Dmitri
  • 153
  • 1
  • 10

1 Answers1

15

Do you know what language are you using in your #lang line? The rest of the instructions below are assuming that you're using a #lang line.


If you are in #lang r5rs and you display or write the values, you should see the output you expect.

> (define p (list 1 2))
> (display p)
(1 2)
> (set-car! p 'one)
> (display p)
(one 2)

If you just type the values bare in Interactions, DrRacket will print them, and that uses the representation you're seeing. In DrRacket, you can customize the way that values print. Here's the process, step-by-step:

  1. Go to the Language menu, and select Choose Language. You should see the language dialog pop up.

  2. If the button on the lower left says Show Details, click it, and the dialog window should expand to include customizations.

  3. Look for the Output Style option. There should be four choices: Constructor, Quasiquote, write, and print. Select write style, and then press Ok to confirm the customization.

Once you do this, then:

> (display (list 1 2))
(1 2)
> (write (list 1 2))
(1 2)
> (list 1 2)
{1 2}

It will still print slightly differently than you expect, using curly braces, because it's trying to notate that the list structure is mutable.

If this bothers you, we can fix that. Add the following line near the top of your program (but after the #lang line).

(#%require r5rs/init)

This line pulls in a Racket-specific module called r5rs/init that tries to improve r5rs compliance; in particular, the braces should finally print as round ones for mutable pairs.

> (display (list 1 2))
(1 2)
> (write (list 1 2))
(1 2)
> (list 1 2)
(1 2)
dyoo
  • 11,795
  • 1
  • 34
  • 44
  • Thanks! The write style did the trick. I was using #lang planet neil/sicp. But now with the write style language shows as SICP PLaneT 1.16 custom and says 'read: #lang not enabled in the current context', I had to comment out the #land line to make it work. – Dmitri Feb 20 '12 at 06:17
  • 1
    Cool. I'll follow up with Neil later to see if his package's defaults can be changed so that it's **write** by default. – dyoo Feb 21 '12 at 21:39
  • How do you configure the output style if you're running from a terminal rather than DrRacket? (I'm running Racket via geiser-mode in Emacs). – crowding Sep 10 '14 at 19:41
  • @crowding See: http://docs.racket-lang.org/reference/Writing.html#%28def._%28%28quote._~23~25kernel%29._global-port-print-handler%29%29. For example, you can configure the default print handler to not use expression-style printing by doing *(print-as-expression #f)*. – dyoo Sep 10 '14 at 22:39
  • @dyoo May I know what the outcome of your following up with Neil is? – Daniel Le Sep 14 '16 at 14:04