loops - 使用传递值运行嵌套循环的惯用方式
问题描述
我想做这样的事情
int n=0
for(int i=xs; i<xe; i++){
for(int j=ys; j<ye; j++){
n++
}
}
return n;
以 Clojure 方式。由于所有值都是不可变的,我认为值 n 应该作为(可能)递归函数的参数传递。最好的方法是什么?
解决方案
最接近您的代码的是
(defn f [xs xe ys ye]
(let [n (atom 0)]
(doseq [_ (range xs xe)
_ (range ys ye)]
(swap! n inc))
@n))
user> (f 1 10 2 20)
;;=> 162
但是可变原子方法根本是单一的。
它可能看起来像这样,更像是clojure方式:
(defn f [xs xe ys ye]
(count (for [_ (range xs xe)
_ (range ys ye)]
nil)))
#'user/f
user> (f 1 10 2 20)
;;=> 162
这真的取决于你想要做什么。正如@jas 注意到的那样,计数 n 显然做得更好(* (- xe xs) (- ye ys))
,与您使用的语言无关)
你提到的递归解决方案怎么样,它可能看起来像这样:
(defn f [xs xe ys ye]
(loop [n 0 i xs j ys]
(cond (== j ye) n
(== i xe) (recur n xs (inc j))
:else (recur (inc n) (inc i) j))))
#'user/f
user> (f 1 10 2 20)
;;=> 162
推荐阅读
- excel - 如何从 Maatwebsite/Laravel-Excel 包中的特定表号读取数据?
- python - Pandas:解析 CSV 区分缺失值标记和恰好等于它的字符串
- apache-spark - 如何通过列值的组合对 S3 输出文件进行分区?
- python - Python3 返回 TypeError:预期的字符串或类似字节的对象
- swift - Swift:节头 XIB 文件未锚定到 tableView 的宽度
- excel - 选择多个弹出窗口
- python - Python 请求在发布后返回 IN-QUEUE 状态
- c++ - 无法链接到 Visual Studio 2019 中的 C++ GLFW 依赖项
- angular - 在 Cart APIs 中添加新的自定义查询参数
- json - 如何在反应中使用api从数据中删除图片