Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
试图理解 Clojure 和 Haskell 之间的区别。我有以下代码计算时间序列数字列表的移动平均值:
movavg n [] = [] movavg n (x:xs) = map (/ n') sums where sums = scanl (+) (n' * x) $ zipWith (-) xs (replicate n x ++ xs) n' = fromIntegral n
这在 Clojure 中的惯用版本是什么?
我不明白为什么一个非常字面的翻译不应该是惯用的,即:
(defn movavg [n coll] (when-let [[x & xs] (seq coll)] (map #(/ % n) (reductions + (* n x) (map - xs (concat (repeat n x) xs))))))
特别是具有大量序列函数的代码总是有可能非常接近 Haskell,因为它们很懒惰。
编辑:根据 Justin Kramer 的建议缩短代码。