方案基本循环
我正在尝试编写一个行为方式类似于循环的方案函数。
(loop min max func)
这个循环应该执行范围 min 和 max (整数) 之间的 func
- 像这样的示例之一
(loop 3 6 (lambda (x) (display (* x x)) (newline)))
9
16
25
36
,我定义该函数,因为
( define ( loop min max fn)
(cond
((>= max min) ( ( fn min ) ( loop (+ min 1 ) max fn) ) )
)
)
当我运行代码时,我得到结果,然后发生错误。 我无法处理这个错误。
(loop 3 6 (lambda (x) (display(* x x))(newline)))
9
16
25
36
Backtrace:
In standard input:
41: 0* [loop 3 6 #<procedure #f (x)>]
In utku1.scheme:
9: 1 (cond ((>= max min) ((fn min) (loop # max fn))))
10: 2 [#<unspecified> ...
10: 3* [loop 4 6 #<procedure #f (x)>]
9: 4 (cond ((>= max min) ((fn min) (loop # max fn))))
10: 5 [#<unspecified> ...
10: 6* [loop 5 6 #<procedure #f (x)>]
9: 7 (cond ((>= max min) ((fn min) (loop # max fn))))
10: 8 [#<unspecified> ...
10: 9* [loop 6 6 #<procedure #f (x)>]
9: 10 (cond ((>= max min) ((fn min) (loop # max fn))))
10: 11 [#<unspecified> #<unspecified>]
**utku1.scheme:10:31: In expression `((fn min) (loop # max ...))`:
utku1.scheme:10:31:Wrong type to apply: `#<unspecified>`
ABORT: (misc-error)**
I'm trying to write a scheme func that behaves in a way similar to a loop.
(loop min max func)
This loop should perform the func between the range min and max (integers)
-- one of an example like this
(loop 3 6 (lambda (x) (display (* x x)) (newline)))
9
16
25
36
and I define the function as
( define ( loop min max fn)
(cond
((>= max min) ( ( fn min ) ( loop (+ min 1 ) max fn) ) )
)
)
when I run the code I get the result then an error occur.
I couldn't handle this error.
(loop 3 6 (lambda (x) (display(* x x))(newline)))
9
16
25
36
Backtrace:
In standard input:
41: 0* [loop 3 6 #<procedure #f (x)>]
In utku1.scheme:
9: 1 (cond ((>= max min) ((fn min) (loop # max fn))))
10: 2 [#<unspecified> ...
10: 3* [loop 4 6 #<procedure #f (x)>]
9: 4 (cond ((>= max min) ((fn min) (loop # max fn))))
10: 5 [#<unspecified> ...
10: 6* [loop 5 6 #<procedure #f (x)>]
9: 7 (cond ((>= max min) ((fn min) (loop # max fn))))
10: 8 [#<unspecified> ...
10: 9* [loop 6 6 #<procedure #f (x)>]
9: 10 (cond ((>= max min) ((fn min) (loop # max fn))))
10: 11 [#<unspecified> #<unspecified>]
**utku1.scheme:10:31: In expression `((fn min) (loop # max ...))`:
utku1.scheme:10:31:Wrong type to apply: `#<unspecified>`
ABORT: (misc-error)**
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
cond 表达式的每个子句都具有 (cond expr1 expr2...) 形式,但是当 expr1 和 expr2 应该分开时,您已将它们包裹在括号中。解决方案是在开头指定
begin
:((>= max min) (begin expr1 expr2))
或使begin
隐式:((>= max min) expr1 expr2)
。或者,使用
when
语句而不是cond
可能更有意义,因为您只有一个选择:因为
cond
和 < code>when 是使用if
的宏,两个版本都变成无论如何。
Each clause of a cond expression has the form (cond expr1 expr2...), but you've wrapped both expr1 and expr2 in parens when they should be separate. The solution would be to either specify
begin
at the beginning:((>= max min) (begin expr1 expr2))
or make thebegin
implicit:((>= max min) expr1 expr2)
.Alternatively, it might make more sense to use a
when
statement instead of acond
, since you only have one choice:Because both
cond
andwhen
are macros that useif
, both versions becomeanyways.
这就是问题所在。您想要执行的操作是先执行
(fn min)
,然后再执行(loop (+ min 1 ) max fn)
。它实际上所做的是,它执行(fn min)
,然后执行(loop (+ min 1 ) max fn)
,然后尝试应用的结果>(fn min)
为(loop (+ min 1 ) max fn)
的结果。由于(fn min)
的结果不是函数,因此这是一个错误。要获得您想要的结果,请执行
( begin (fn min) (loop (+ min 1) max fn) )
。begin
允许您依次执行多个表达式。This is the problem. What you want this to do is to execute
(fn min)
and then(loop (+ min 1 ) max fn)
afterwards. What it actually does do is, it executes(fn min)
, then(loop (+ min 1 ) max fn)
and then it tries to apply the result of(fn min)
to the result of(loop (+ min 1 ) max fn)
. Since the result of(fn min)
is not a function, this is an error.To get what you want, do
( begin (fn min) (loop (+ min 1) max fn) )
.begin
allows you to execute multiple expressions after one another.