球拍中的mcons

问题描述:

我在读取球拍博士的输出时遇到问题.默认情况下,它使用mcons显示列表.例如,sicp练习2.32产生:

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))

谢谢!

您知道您在#lang行中使用的语言吗?以下其余说明假定您使用的是#lang行.

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.

如果您位于#lang r5rs中,并且您displaywrite值,您应该会看到期望的输出.

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)

如果您仅在交互"中键入空值,DrRacket将print将其使用,并使用您所看到的表示形式.在DrRacket中,您可以自定义print的方式.这是逐步的过程:

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. 转到语言菜单,然后选择选择语言.您应该会看到语言对话框弹出.

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

如果左下方的按钮显示显示详细信息,请单击它,然后对话框窗口应展开以包含自定义项.

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

查找输出样式选项.应该有四个选择:构造器准引用编写打印.选择样式,然后按确定以确认自定义.

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.

执行此操作后,

> (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.

如果这困扰您,我们可以解决.在程序顶部附近(但在#lang行之后)添加以下行.

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)

此行引入了特定于球拍的模块,称为 r5rs/init r5rs合规性;特别是,大括号最终应打印为圆形,以便于可变对.

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)