- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在 Clojure 中尝试一些惰性流。如果我这样做:
(defn ints-from [n]
(cons n (lazy-seq (ints-from (inc n)))))
和
(def nats (ints-from 0))
没关系,我可以这样做:
(take 5 nats)
现在我试图将 2 个函数封装在 1 中:
(defn natz[]
( letfn [(aux [n]((cons n (lazy-seq (aux (inc n)))))) ] (aux 0) ))
这似乎可以编译,但没有达到我的预期。
(take 4 natz)
给出:
(user=> IllegalArgumentException Don't know how to create ISeq from: user$natz
clojure.lang.RT.seqFrom (RT.java:494)
我错过了什么?
最佳答案
letfn定义中少一个括号,调用natz函数多一个括号
(defn natz[]
(letfn [(aux [n] (cons n (lazy-seq (aux (inc n)))))]
(aux 0)))
示例用法:
(take 4 (natz))
=> (0 1 2 3)
关于recursion - clojure 惰性序列和 letfn,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20837548/
我正在 Clojure 中尝试一些惰性流。如果我这样做: (defn ints-from [n] (cons n (lazy-seq (ints-from (inc n))))) 和 (def
我正在 Clojure 中尝试一些惰性流。如果我这样做: (defn ints-from [n] (cons n (lazy-seq (ints-from (inc n))))) 和 (def
我花了几年时间“回到过去”做 Scheme,现在正在学习 Clojure。 Scheme 中的“最佳实践”之一是在父函数中定义辅助函数,从而限制了它们从“外部”的可见性。当然,当时 TDD 还没有完成
Clojure 风格(以及一般良好的软件工程)强调许多小函数,其中的一个子集是公开可见的,以提供外部接口(interface)。 在 Clojure 中似乎有几种方法可以做到这一点: (letfn [
在实践中,我什么时候应该使用 letfn 与 let 来定义本地函数?如果我既想要局部函数又想要局部非函数怎么办? 最佳答案 如果我只需要一个或几个本地函数,我就会 letfn 它们。如果我需要定义函
假设定义了 abs 和 avg: (defn sqrt [x] (letfn [(s [guess] (if (good-enough? guess)
我是一名优秀的程序员,十分优秀!