【问题标题】:Clojure - displaying some output correctlyClojure - 正确显示一些输出
【发布时间】:2012-10-20 18:08:38
【问题描述】:

我对 clojure 还很陌生,所以仍在努力学习……还有很多东西。现在我坚持试图让一些输出正确显示。这是我的功能:

;function 7 - display the board
(defn display [small medium large]
(let [board (loop [i 0 boardVector [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]]
          ;(prn (bit-test small i)(bit-test medium i)(bit-test large i))
          (if (< i 16)
           (cond
             ;nothing exists
             (and (not(bit-test small i))(not(bit-test medium i))(not(bit-test large i)))
               (recur (+ i 1) (assoc boardVector i (+ (nth boardVector i) 0)))
             ;only small bit exists
             (and (bit-test small i)(not(bit-test medium i))(not(bit-test large i)))
               (recur (+ i 1) (assoc boardVector i (+ (nth boardVector i) 1)))
             ;small and medium exists on square
             (and (bit-test small i)(bit-test medium i)(not(bit-test large i)))
               (recur (+ i 1) (assoc boardVector i (+ (nth boardVector i) 3)))
             ;only medium exists on square
             (and (not(bit-test small i))(bit-test medium i)(not(bit-test large i)))
               (recur (+ i 1) (assoc boardVector i (+ (nth boardVector i) 2)))
             ;medium and large exists on square
             (and (not(bit-test small i))(bit-test medium i)(bit-test large i))
               (recur (+ i 1) (assoc boardVector i (+ (nth boardVector i) 6)))
             ;only large exists on square
             (and (not(bit-test small i))(not(bit-test medium i))(bit-test large i))
               (recur (+ i 1) (assoc boardVector i (+ (nth boardVector i) 4)))
             ;all three exists on square
             (and (bit-test small i)(bit-test medium i)(bit-test large i))
               (recur (+ i 1) (assoc boardVector i (+ (nth boardVector i) 7)))
               )
           boardVector)
         )
    ]
(prn  board)
)
)

这是我用来作为参数传递的内容:

(display 2r1001000100 2r101001000 2r111000000)

此函数采用位变量(长度不超过 16...),并根据设置的位计算最终向量。 a small 值 1,medium 值 2,large 值 4。这就是为什么我有那个条件语句来概述每种不同的可能性,不包括同时设置 small 和 large 且没​​有 medium set 的可能性。

无论如何,如果这些都没有意义,那么这是实际的问题: 如何获得显示为的当前输出:

[0 0 1 2 0 0 7 4 6 1 0 0 0 0 0 0]
nil

让它这样显示?

0 0 1 2
0 0 7 4
6 1 0 0
0 0 0 0

我已经尝试过使用诸如 apply、map、str、interpose 之类的函数……但我永远无法弄清楚我到底做错了什么。目前我将输出显示为副作用(我认为这是正确的名称......),但它总是从 prn 函数的输出返回 nil 。也不知道如何摆脱它。非常感谢任何帮助!

【问题讨论】:

  • 请不要将右括号放在自己的行中。对于有经验的 Clojurians 来说,这让代码很难阅读。

标签: clojure


【解决方案1】:

将其分成 4 个组,然后打印每个组:

(dorun (map #(apply println %) (partition-all 4 r)))
0 0 1 2
0 0 7 4
6 1 0 0
0 0 0 0

或使用for 表达式:

(dorun (for [line (partition-all 4 r)] (apply println line)))
0 0 1 2
0 0 7 4
6 1 0 0
0 0 0 0

或使用doseq

(doseq [line (partition-all 4 r)] (apply println line))
0 0 1 2
0 0 7 4
6 1 0 0
0 0 0 0

dorun(或doall)对于formap 示例是必需的,因为它们是惰性的,否则实际上不会打印任何内容,除非它们返回的序列被消耗。使用println 调用apply 和序列会导致println 将序列的内容作为参数而不是它自身的序列,这样可以防止打印额外的括号。

【讨论】:

  • 无法解释我对此的感谢!你们中的一些大师在这方面真的给我留下了深刻的印象。谢谢!
猜你喜欢
  • 2013-02-25
  • 2019-01-05
  • 2018-11-15
  • 2018-01-17
  • 2013-05-15
  • 2023-03-26
  • 1970-01-01
  • 2021-10-01
  • 2017-04-04
相关资源
最近更新 更多