Scheme中命令的执行顺序
我正在方案中编写一个程序,它使用递归来遍历列表,并在计数器达到某个数字N时停止在某个指针处
(define (functX N lst)
(define counter 1)
(cond
[(empty? lst) empty]
[(negative? N) empty]
[(< (length lst) N) empty]
[(<= counter N) ((set! counter (+ counter 1))(cons (first lst) (functX N (rest lst)))))]
[else empty]))
我不明白,为什么从底部开始的第二行给我带来麻烦:错误我得到的是“程序应用程序:预期程序,给定:'(1)(无参数)”
I am writing a program in scheme, that uses recursion to walk through the list, and stop at certain pointer, when counter reaches a certain number N
(define (functX N lst)
(define counter 1)
(cond
[(empty? lst) empty]
[(negative? N) empty]
[(< (length lst) N) empty]
[(<= counter N) ((set! counter (+ counter 1))(cons (first lst) (functX N (rest lst)))))]
[else empty]))
I don't understand, why the second line from the bottom is giving me trouble: the error I am getting is "procedure application: expected procedure, given: '(1) (no arguments)"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您将其括在括号中两次。 Scheme 中的表达式的格式为
(func-expr arg-expr ...)
,因此第一个表达式必须计算为函数。因此,如果您这样做:它将评估
(f 5)
然后它会尝试评估(5)
这是一个错误。编辑:一些说明。
您将以下内容括在括号中两次:
因此首先它计算
set!
并减少到(其中n
是一个数字):cons
是然后与其参数一起求值(其中x
是结果):然后它尝试将参数
x
应用于函数n
,但由于n
是一个会导致错误的数字。如果您想进行两次单独的计算并且只返回一个值,则可以使用begin
。更新:
这是一个函数,它似乎可以在没有巫毒的情况下做你想做的事情(因为突变是邪恶的)。
You have it enclosed in parentheses twice. Expressions in Scheme have the form
(func-expr arg-expr ...)
, so the first expression must evaluate into a function. So if you did:It would evaluate
(f 5)
then it would try and evaluate(5)
which is an error.Edit: Some clarification.
You have the following enclosed in brackets twice:
So first it evaluates
set!
and reduces down to (wheren
is a number):cons
is then evaluated along with its arguments (wherex
is the result):It then tries to apply the argument
x
to the functionn
, but sincen
is a number this results in an error. If you wanted to do two separate computations and only return the value of one, you can usebegin
.Update:
Here is a function that appears to do what you want without voodoo (since mutation is evil).
您应该考虑在递归调用中递减 N 并完全删除
counter
变量。You should consider decrementing N in the recursive call and removing the
counter
variable altogether.