如何在 Clojure 中通过 def 定义的序列添加元/注释?
我必须注释掉下面的行(示例来自 http://en.wikibooks.org /wiki/Clojure_Programming/Examples/Lazy_Fibonacci)
(def fib-seq
;"Implements Fibonacci sequence (starts with 0)."
((fn rfib [a b]
(lazy-seq (cons a (rfib b (+ a b)))))
0 1))
如果我把它留在里面,我会得到:
Clojure 1.2.0
java.lang.Exception: Too many arguments to def (Problem1.clj:1)
1:1 user=>
但是,我可以使用 defn
来做到这一点。示例(我知道,我正在为 even?
重新发明轮子已经定义):
(defn is-even? [n]
"Returns true if the number is even, false otherwise."
(== (mod n 2) 0))
Clojure 1.2.0
1:1 user=> (is-even? 3)
false
1:2 user=> (is-even? 4)
true
1:3 user=>
I had to comment out the line below (example is from http://en.wikibooks.org/wiki/Clojure_Programming/Examples/Lazy_Fibonacci)
(def fib-seq
;"Implements Fibonacci sequence (starts with 0)."
((fn rfib [a b]
(lazy-seq (cons a (rfib b (+ a b)))))
0 1))
If I left it in, I would get:
Clojure 1.2.0
java.lang.Exception: Too many arguments to def (Problem1.clj:1)
1:1 user=>
I can do this with defn
, however. Example (I know, I am reinventing the wheel for even?
is already defined):
(defn is-even? [n]
"Returns true if the number is even, false otherwise."
(== (mod n 2) 0))
Clojure 1.2.0
1:1 user=> (is-even? 3)
false
1:2 user=> (is-even? 4)
true
1:3 user=>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
(def ^{:doc "延迟实现 Fib. 序列。"} fibs ...)
(:doc (meta (var fibs)))
; “延迟实现 Fib. 序列。”
编写宏非常简单,因此您可以编写
(def-with-docs foo "doc" 1)
。(:doc (meta (var fib-seq)))
; “实现斐波那契数列(从 0 开始)。”
另外,请注意,在使用
defn
的示例中,文档字符串应位于参数之前,否则它将不会与符号的相关联元数据。或者,可以使用 clojure.contrib.def/ defvar。
(def ^{:doc "Implements Fib. sequence lazily."} fibs ...)
(:doc (meta (var fibs)))
; "Implements Fib. sequence lazily."
It's simple enough to write a macro so you could write
(def-with-docs foo "doc" 1)
.(:doc (meta (var fib-seq)))
; "Implements Fibbonaci sequence (starts with 0)."
Also, note that with your example use of
defn
, the docstring should precede the arguments, else it won't be associated with the symbol's metadata.Alternatively, one can use clojure.contrib.def/defvar.
这个问题已经在 Clojure 1.3 的新 alpha 版本中“修复”,其中 def 支持可选的文档字符串。
This issue is "fixed" already in the new alpha releases for Clojure 1.3, where def supports an optional docstring.
来自 http://clojure.org/special_forms#def
来自 http://clojure.github.com/clojure/ clojure.core-api.html#clojure.core/defn
因此,当您编写 (def fib-seq "your comment" (...)) 时,您试图定义一个值为“your comment”的符号 fib-seq,并且 clojure 抱怨参数太多。
From http://clojure.org/special_forms#def
From http://clojure.github.com/clojure/clojure.core-api.html#clojure.core/defn
So, when you write (def fib-seq "your comment" (...)), you are trying to define a symbol fib-seq with value "your comment", and clojure complains there are too many arguments.