Racket的“范围内”不能按预期工作

时间:2016-07-15 04:22:18

标签: scheme racket

为什么以下导致错误?

(println ((in-range 1 10 2)))

;; results in:

application: not a procedure;
 expected a procedure that can be applied to arguments
  given: #<stream>
  arguments...: [none]

println不是程序吗?

以下按预期方式工作:

(for (( x  (in-range 1 10 2))) (println x))

输出:

1
3
5
7
9
> 

1 个答案:

答案 0 :(得分:3)

您的代码有两个不正确的括号 - 请记住:在方案()中,表达式意味着过程应用程序(这就是错误说明的内容);试试这个:

(println (in-range 1 10 2))
=> #<stream>

小心!以上输出;它适用于第二个片段,因为for会消耗它。如果您打算在给定范围内打印值列表,则可以采用以下方法:

(println (range 1 10 2))
=> '(1 3 5 7 9)
相关问题