mcons in dr racket
Asked Answered
R

1

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!

Rauscher answered 19/2, 2012 at 6:37 Comment(0)
M
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)
Misuse answered 19/2, 2012 at 8:12 Comment(5)
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.Rauscher
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.Misuse
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).Phrygia
@Phrygia See: docs.racket-lang.org/reference/…. For example, you can configure the default print handler to not use expression-style printing by doing (print-as-expression #f).Misuse
@Misuse May I know what the outcome of your following up with Neil is?Lager

© 2022 - 2024 — McMap. All rights reserved.