1

I'm quite new to clojure so still trying to learn... well a LOT of stuff. Right now I'm stuck on trying to get some output to display properly. Here is my function:

;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)
)
)

Here is what I'm using to pass as arguments:

(display 2r1001000100 2r101001000 2r111000000)

This function takes bit variables (that are no more than 16 in length...), and calculates a final vector depending on which bits are set. a small is worth 1, medium worth 2, large worth 4. This is why I have that condition statement outlining each different possibility, excluding the possibility of having a small and large, and no medium set at the same time.

Anyhow, if none of that makes sense, here's the actual question: How do I get my current output that displays as:

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

To make it display like this?

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

I've tried using functions such as apply, map, str, interpose... but I never could figure out what the heck I was doing wrong with them. Currently I have the output displayed as a side-effect (i think that is the correct name for that...), but it's always returning nil from the output of the prn function. Dunno how to get rid of that either. Would appreciate any help!

1
  • Please don't put closing parentheses on their own lines. It makes code really hard to read for experienced Clojurians. Commented Oct 21, 2012 at 1:14

1 Answer 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.

Sign up to request clarification or add additional context in comments.

1 Comment

Can't explain my thanks for this! Some of you gurus at this just really impresses me. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.