首页 文章

什么'(清单1 2)在Scheme中意味着什么?

提问于
浏览
0

我正在研究SICP,在2.2.2节的开头,它提供了以下代码: (cons '(list 1 2) (list 3 4))) 并说它构造了一个像 ((1 2) 3 4) 这样的列表 . 但是当我输入DrRacket(我实际上在这里使用Racket)时它产生 '((list 1 2) 3 4) 并且如果我写 (cons (list 1 2) (list 3 4)) 那么它会没问题 . 我知道Scheme '(1 2) 等于 (list 1 2)'(list 1 2) 是什么意思?

4 回答

  • 3

    符号 'foo 生成一个名为foo的符号 .

    符号 '(foo bar) 生成一个列表,其中包含两个名为 foobar 的符号 .

    以同样的方式 '(list foo bar) 列出三个符号 . 符号 'list 碰巧被称为 list .

    现在 (list 'foo 'bar) 列出了两个名为 foobar 的符号 .

  • 2

    它应该是“由原子列表,原子1和原子2组成的列表” . 在Scheme评估列表(单引号阻止)之前,它不会将“list”与任何其他字符串区别对待 .

  • 1

    Scheme有一个方便的语法来表示数据文字:使用'(单引号)为任何表达式加前缀而表达式而不是被评估的表达式将作为数据返回

    有关更多信息:

    http://courses.cs.washington.edu/courses/cse341/04wi/lectures/14-scheme-quote.html

  • 2

    Fix output style

    首先,当您在DrRacket中使用 #!racket 语言时,默认的打印方式不是打印它的表示,而是打印得到的表达式 . 您可以从菜单 language >> choose language 关闭它 . 您选择显示详细信息,然后在输出样式下选择 write

    按Run后,在评估 'test 时,您将获得输出 test .

    Typo in expression

    section 2.2.2中有一个表达式 (cons (list 1 2) (list 3 4)) . 这是你在问题中所写的 not the same(cons '(list 1 2) (list 3 4)) . 虽然表达式 (list 1 2) 将过程 list 应用于值 12 ,因此变为 (1 2) ,但表达式 '(list 1 2) 只返回引用数据 (list 1 2) 不变 .

    从而:

    (cons (list 1 2) (list 3 4))   ; ==> ((1 2) 3 4)
    (cons '(list 1 2) (list 3 4))  ; ==> ((list 1 2) 3 4)
    '(cons '(list 1 2) (list 3 4)) ; ==> (cons '(list 1 2) (list 3 4))
    

相关问题