gpt4 book ai didi

scheme - 在 do 循环中填充列表返回列表为空

转载 作者:太空宇宙 更新时间:2023-11-03 18:56:55 24 4
gpt4 key购买 nike

我正在学习Racket,我不知道为什么lst总是空的:

#lang racket

(define sort-asc-by-second
(lambda (lst)
(sort lst
(lambda (x y) (< (cdr x) (cdr y))))))

(define sum
(lambda (lst)
(apply + (map cdr lst))
)
)

(define greater-than
(lambda (lst y)
(cond ((null? lst) (void))
((>= (cdr (car lst)) y) (car lst))
(else (greater-than (cdr lst) y)))))

(define my-procedure
(lambda (lst)
(define sorted (sort-asc-by-second lst))
(define suma (sum lst))
(define probabilidades (map (lambda (p) (cons (car p) (/ (cdr p) suma))) sorted))

(define prob (random))

(car (greater-than lst prob))
)
)
(define lst '())

(do ([x 6000 (- x 1)]
(set! lst (my-procedure '((a . 1)(b . 2)(c . 3)))))
((zero? x) lst))

(display lst)

我想修改 do 中的 lst 列表,但它没有。

你知道为什么lst是空的吗?

更新
我测试了以下指令,结果如下:

> (my-procedure '((a . 1)(b . 2)(c . 3)))
'a

> (set! lst (my-procedure '((a . 1)(b . 2)(c . 3))))
> (display lst)
'a

我仍然不知道为什么在循环时 lst 是空的。

最佳答案

do-形式是

(do (initialisers)
(stop-condition end-expression)
body)

但是你写了

(do (initialiser body)
(stop-condition end-expression))

不幸的是,(my-procedure '((a . 1)(b . 2)(c . 3))) 的值已绑定(bind)到名称 set! 在循环中,lst 永远不会被修改。

关于scheme - 在 do 循环中填充列表返回列表为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52962046/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com