Scheme 无法使用租入球拍
我正在尝试修改将let用于插入函数的排序代码:Scheme 无法使用租入球拍,scheme,racket,let,Scheme,Racket,Let,我正在尝试修改将let用于插入函数的排序代码: (define (mysort alon ) (let insert ((n n) (alon alon)) (cond [(empty? alon) (cons n empty)] [else (cond [(< n (first alon)) (cons n alon)] [else (cons (first alon)
(define (mysort alon )
(let insert ((n n) (alon alon))
(cond
[(empty? alon) (cons n empty)]
[else (cond
[(< n (first alon)) (cons n alon)]
[else (cons (first alon)
(insert n (rest alon))])])
(cond
[(empty? alon) empty]
[(cons? alon) (insert (first alon)
(mysort (rest alon)))])))
(mysort (list 1 2 3 4 5 6 2 3 1 4 5 2 10))
我在这里看到“let”需要有变量的初始值。我们可以在不初始化变量的情况下使用“let”吗?如何更正上述代码
编辑:我尝试使用lambda,但不起作用:
(define (mysort4 alon )
(let ([insert4
(lambda (n alon)
(cond
[(empty? alon) (cons n empty)]
[(< n (first alon)) (cons n alon)]
[else (cons (first alon)
(insert4 n (rest alon) ))]))])
(cond
[(empty? alon) empty]
[(cons? alon) (insert4 (first alon)
(mysort4 (rest alon) ) )])))
(mysort4 (list 1 2 3 4 5 6 2 3 1 4 5 2 10))
当需要内部辅助函数时,请使用内部定义而不是let。 使用“定义”而不是“允许您获得”进行最小更改:
当你用let创建一些东西时 显然,lambda内部的测试是全局的,而不是它本身,但为什么呢。下面是立即调用的匿名函数的语法糖,我们可以将其重写为:
(define test 10)
((lambda (test)
(test 'result))
(lambda (x)
(list x test)))
这里您可以看到,第一个lambda将test作为绑定变量,因此始终是第一个操作数,但第二个lambda除了全局绑定之外没有test
在递归过程中,不能使用let,因为创建闭包时绑定不在环境中,并且在计算lambda时也不在环境中。要解决此问题,我们使用letrec解决此问题:
(define test 10)
(letrec ((test (lambda (x)
(list x test))))
(test 'result))
; ==> (result #<procedure:test>)
在命名let中,名称绑定在letrec中,但其他值不绑定。要修复第一个问题,请执行以下操作:
(define (mysort alon)
(cond
[(empty? alon) empty]
[(cons? alon)
(let insert ((n (first alon))
(alon (rest alon)))
(cond
[(empty? alon) (cons n empty)]
[(< n (first alon)) (cons n alon)]
[else (cons (first alon) (insert n (rest alon)))]))]))
请注意,我展平了嵌套的cond,因为cond的整个点不必嵌套它们。它相当于其他语言中的if-elseif*-else。代码不起作用,因为它只放置第一个元素。Pehaps在一个列表中插入所有元素,从一个空列表开始就可以了
第二种方法是将let改为letrec。该代码与只根据其余元素插入第一个元素的功能相同,但递归将适用于该元素
如果您查看链接到的页面,您将看到您插入的列表已排序。你错过了一些东西
插入排序不是一种有效的算法。LangRacket对较小的列表使用合并排序和调整。试图在速度上击败它将是一件愚蠢的差事
*反之亦然。racket使用letrec-letrec值来精确如果没有变量的初始值,则命名为let的将只是一个lambda。我无法在上述代码中插入lambda关键字。let insert lambda n alon和let insert lambda n alon都不起作用。let[insert4 lambda n alon…]也不起作用。我知道define起作用了,但我想知道在这种情况下是否有任何方法可以使用“let”。显然,我不能。所以你真正想问的是,命名让如何工作?看看:
(define test 10)
(let ((test (lambda (x)
(list x test))))
(test 'result))
; ==> (result 10)
(define test 10)
((lambda (test)
(test 'result))
(lambda (x)
(list x test)))
(define test 10)
(letrec ((test (lambda (x)
(list x test))))
(test 'result))
; ==> (result #<procedure:test>)
(let ((test 'undefined))
(let ((newtemp (lambda (x) (list x test))))
(set! test newtemp))
(test 'result))))
; ==> (result #<procedure:test>)
(let () ; this is to make `define` local and not global
(define test (lambda (x)
(list x test))
(test 10))
; ==> (result #<procedure:test>)
(define (mysort alon)
(cond
[(empty? alon) empty]
[(cons? alon)
(let insert ((n (first alon))
(alon (rest alon)))
(cond
[(empty? alon) (cons n empty)]
[(< n (first alon)) (cons n alon)]
[else (cons (first alon) (insert n (rest alon)))]))]))