方案基本循环

发布于 2024-10-09 22:42:07 字数 1324 浏览 0 评论 0原文

我正在尝试编写一个行为方式类似于循环的方案函数。

(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦年海沫深 2024-10-16 22:42:07

cond 表达式的每个子句都具有 (cond expr1 expr2...) 形式,但是当 expr1 和 expr2 应该分开时,您已将它们包裹在括号中。解决方案是在开头指定 begin((>= max min) (begin expr1 expr2)) 或使 begin 隐式:((>= max min) expr1 expr2)

或者,使用 when 语句而不是 cond 可能更有意义,因为您只有一个选择:

(define (loop min max fn)
  (when (>= max min)
    (fn min)
    (loop (+ min 1) max fn)))

因为 cond 和 < code>when 是使用 if 的宏,两个版本都变成

(define (loop min max fn)
  (if (>= max min)
    (begin (fn min) (loop (+ min 1) max fn))
    (void)))

无论如何。

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 the begin implicit: ((>= max min) expr1 expr2).

Alternatively, it might make more sense to use a when statement instead of a cond, since you only have one choice:

(define (loop min max fn)
  (when (>= max min)
    (fn min)
    (loop (+ min 1) max fn)))

Because both cond and when are macros that use if, both versions become

(define (loop min max fn)
  (if (>= max min)
    (begin (fn min) (loop (+ min 1) max fn))
    (void)))

anyways.

゛时过境迁 2024-10-16 22:42:07
( ( fn min ) ( 循环 (+ min 1 ) max fn) )

这就是问题所在。您想要执行的操作是先执行 (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 允许您依次执行多个表达式。

( ( fn min ) ( loop (+ min 1 ) max fn) )

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文