gpt4 book ai didi

scheme - 如何在这里正确地增加变量?

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

(define (teken-n x y n)
(if (not (zero? n))
(begin
(teken-symbolen x y)
(teken-n x y (- n 1)))))

(define (teken-symbolen-combinatie x y n)
(if (not (zero? n))
(begin
(let ((acc 0))
(teken-n x y (+ acc 1))
(newline)
(teken-symbolen-combinatie x y (- n 1))))))

使用(teken-n x y n)是打印2个符号,'x 'yxyx的形式打印n次1 行,没有任何空格。它工作正常。

我试图用 (teken-symbolen-combinatie x y n) 完成的输出是

(teken-symbolen-combinatie '- '* 3)
-*-
-*--*-
-*--*--*-

我的代码只给出了第一个符号组合,在这个例子中 -*- 用换行符分隔 3 次。这意味着我的第一个函数的最后一个变量没有正确递增。我不明白为什么。

编辑:更正问题中的错误

最佳答案

没有增加的是acc。函数 + 返回一个新值,它不会更改任何参数。

Scheme 中通常的方法是让一个内部循环有另一个变量(这似乎是您的 acc 的目的)增加到 n。您不需要外部函数循环。

关于scheme - 如何在这里正确地增加变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52746517/

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