Question

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!

Was it helpful?

Solution

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)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top