方案中是什么?我们如何使用它?
scm> (define (x) 100)
x
scm> (x)
100
scm> x ; When we "called" x, it return (lambda () 100). what is it ?
(lambda () 100)
Run Code Online (Sandbox Code Playgroud) 我一直无法很好地理解键绑定语法(如果您不知道符号的名称,就很难用谷歌搜索!)。
在 DrRacket 中,我看到诸如“c:g”或“esc;g”之类的绑定……我尝试敲击这些键但无济于事。我试过同时打它们。似乎没有任何效果。
他们的意思是什么?而且,一般来说,人们如何理解这种语法?
我想要做的是定义一个列表,例如 (define lst '(1 2 3 4 5 6)) 然后调用 (split lst) 它将返回 '((1 3 5) (2 4 6))。
一些例子:
lst是'(1 2 3 4 5 6),它应该返回'((1 3 5) (2 4 6))lst是'(1 2 3 4 5 6 7),它应该返回'((1 3 5 7) (2 4 6))lst是'("a" "little" "bit" "of" "that" "to" "spice" "things" "up"),它应该返回'(("a" "bit" "that" "spice" "up") ("little" "of" "to" "things"))它应该在构建两个列表时交替。所以第一个索引应该在第一个列表中,第二个索引在第二个列表中,第三个索引在第一个列表中,依此类推。
这是我当前的脚本。
(define (split …Run Code Online (Sandbox Code Playgroud) Scheme 和 Racket 不保护特殊形式,因此可以重新定义它们。纯粹出于好奇,有没有办法在被覆盖后取回特殊形式?
示例情况:
$ racket
Welcome to Racket v6.11.
> (define define 1)
> (+ define define)
2
> (define x 3) ; How to get the original 'define' back?
; x: undefined;
; cannot reference undefined identifier
; [,bt for context]
>
Run Code Online (Sandbox Code Playgroud)
如果我重新定义一个特殊形式,有没有办法找回特殊形式?
有人可以解释以下表达式吗
> (+)
0
> (+ 1)
1
> (- 1)
-1
> (/ 1)
1
> (/ 2)
1/2
> (/ 3)
1/3
Run Code Online (Sandbox Code Playgroud)
如果默认参数为 1,为什么(+ 1)return 1while (/ 2)return 1/2?
不应该(+ 1)回来2吗?
我有以下代码:
(define numbers '(2 3 5 3 1 22 2))
(define (count val l)
(if (null? l)
0
(+
(if (= (first l) val) 1 0)
(count val (rest l))
)
)
)
(display (count 6 numbers))
Run Code Online (Sandbox Code Playgroud)
(对不起,如果我的代码看起来很糟糕,只需要使用这种语言一次)
编译器说:
count: contract violation
expected: procedure?
given: 6
argument position: 1st
other arguments...:
'(3 5 3 1 22 2)
Run Code Online (Sandbox Code Playgroud) 不是在 Lisp 中创建新列表list的关键字,但在 Lisp 中可以调用一个参数list。我认为大多数编程语言(如 Java 或 C++)中的关键字不能用于参数 名称,Lisp 中是否有特殊原因可以使用?
在谈论 Painters 时,SICP 使用定义如下的程序:
(define (segments->painter segment-list)
...
)
Run Code Online (Sandbox Code Playgroud)
不幸的是,文本并没有(还?)说明什么->代表什么。->Scheme中的用法是什么?例如,显示段将被视为画家的“方法”(或者反之亦然)是否是 OOP 风格的事情?我唯一见过这个->符号的地方是在 C 中,当然我们在这里不处理指针,所以我有点不清楚它是如何使用的或它在什么约定中使用。
在 Racket 中,一种点分隔数字(例如软件的版本号)如何?
例如
'("1.1.2" "1.0.0" "1.3.3" "1.0.7" "1.0.2")
Run Code Online (Sandbox Code Playgroud)
分类为
'("1.0.0" "1.0.2" "1.0.7" "1.1.2" "1.3.3")
Run Code Online (Sandbox Code Playgroud) racket ×10
scheme ×9
lisp ×3
arguments ×1
contract ×1
function ×1
key-bindings ×1
mit-scheme ×1
sicp ×1