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