如何正确缩进 clojure/lisp?

发布于 2024-11-15 12:27:37 字数 705 浏览 5 评论 0原文

我想缩进下面的代码。 liper 会如何缩进这个? 我特别困惑在哪里放置换行符。

(defn primes [n]
  (letfn [(sieve [table removal]
                 (assoc table removal false))
          (primebools [i table]
                       (cond 
                         (= i n) table 
                         (table i) (recur (inc i) 
                                          (reduce sieve 
                                                  table 
                                                  (range (* i i) n i))) 
                         :else (recur (inc i) 
                                      table)))]
    (let [prime? (primebools 2 (apply vector (repeat n true)))]
      (filter prime? (range 2 n)))))

I want to indent the following piece of code.
How would a lisper indent this?
I am especially confused about where to put newlines.

(defn primes [n]
  (letfn [(sieve [table removal]
                 (assoc table removal false))
          (primebools [i table]
                       (cond 
                         (= i n) table 
                         (table i) (recur (inc i) 
                                          (reduce sieve 
                                                  table 
                                                  (range (* i i) n i))) 
                         :else (recur (inc i) 
                                      table)))]
    (let [prime? (primebools 2 (apply vector (repeat n true)))]
      (filter prime? (range 2 n)))))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

荒人说梦 2024-11-22 12:27:37
(defn primes [n]
  (letfn [(sieve [table removal]
            (assoc table removal false))
          (primebools [i table]
            (cond 
              (= i n) table 
              (table i) (recur (inc i) 
                          (reduce sieve table 
                            (range (* i i) n i))) 
              :else (recur (inc i) table)))]
    (let [prime? (primebools 2 (apply vector (repeat n true)))]
      (filter prime? (range 2 n)))))

我就是这么做的。

(defn primes [n]
  (letfn [(sieve [table removal]
            (assoc table removal false))
          (primebools [i table]
            (cond 
              (= i n) table 
              (table i) (recur (inc i) 
                          (reduce sieve table 
                            (range (* i i) n i))) 
              :else (recur (inc i) table)))]
    (let [prime? (primebools 2 (apply vector (repeat n true)))]
      (filter prime? (range 2 n)))))

Is how I would do it.

趁微风不噪 2024-11-22 12:27:37

除了 @dnolen 的回答之外,当有

  1. 一个新函数(如前两行)
  2. 时,我通常会放置一个新行来缩进函数的长或重要参数(如 cond 块),
  3. 逻辑上保留每个行数不要超过 80 个字符,并将较长的想法分解为较小的块
  4. ,最重要的是,保持一致!

然后只需对齐和缩进行,以便标识具有相同的代码深度。

In addition to @dnolen's answer, I usually put a new line when there's

  1. a new function (like your first two lines)
  2. to indent long or important argument to a function (like the cond block)
  3. logically keep each line to less than 80 characters and break up long ideas to smaller chunks
  4. most importantly, be consistent!

Then just align and indent lines so that the identations are for the same depth of code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文