1

我对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 。也不知道如何摆脱它。将不胜感激任何帮助!

4

1 回答 1

1

partition it into groups of 4, then print each group:

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

or using a for expression:

(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

or using a 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 (or doall) is required for the for and map examples because these are lazy and would otherwise not actually print anything unless the sequence they return was consumed. Calling apply with println and the sequence causes println to get the contents of the sequence as arguments instead of the sequence it's self, which keeps it from printing extra parens.

于 2012-10-20T18:50:09.113 回答