Clojure - displaying some output correctly - clojure

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!

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.

Related

Clojure Fibonacci series using while in atom

How to achieve this using Clojure atom on the Fibonacci series.
(def x 0)
(def y 0)
(def z 1)
(defn problem []
(def a (atom 0))
(while (< #a 10 )
(do
;logic code need to write to achieve the output
; how can I add b,c == c,b???????.)
(swap! a inc)))
(problem)
output should be 0 1 1 2 3 5 8 13 21 34 55
As mentioned in the comments, defining and using an atom in a function is a totally non-clojure way of solving the question which makes it all the more difficult to solve. You should get familiar with the concept of immutability and higher order functions like map, filter, reduce, etc.
The code below gives you what you want.
(def fib1
(fn [n]
(cond
(= n 0) 1
(= n 1) 1
:else (+ (fib1 (dec n)) (fib1 (- n 2))))))
since this gives you just nth Fibonacci number, you should take + map with it if you want to get what you want.
(take 10 (map fib1 (range))) => (1 1 2 3 5 8 13 21 34 55)
take a look at this beautiful solution I found on internet.
(def fib-seq-iterate (map first (iterate (fn [[n m]] [m (+ n m)]) [0 1])))
This returns a lazy sequence, which can be called with
(take 5 fib-seq-iterate) => (0 1 1 2 3)
(def x 0)
(def y 0)
(def z 1)
(defn problem []
(def a (atom 0))
(while (< #a 10 )
(do
(println x)
(def x (+ y z))
(def y z)
(def z x)
(swap! a inc))))
(problem)
Output:
0 1 2 3 5 8 13 21 34 55

Clojure flat sequence into tree

I have the following vector, [-1 1 2 -1 3 0 -1 2 -1 4 0 3 0 0]
which represents the tree [[1 2 [3] [2 [4] 3]]]
where -1 begins a new branch and 0 ends it. How can I convert the original vector into a usable tree-like clojure structure (nested vector, nested map)? I think clojure.zip/zipper might do it but I'm not sure how to build those function args.
Zippers are a good tool for this:
(require '[clojure.zip :as zip])
(def in [-1 1 2 -1 3 0 -1 2 -1 4 0 3 0 0])
(def out [[1 2 [3] [2 [4] 3]]])
(defn deepen [steps]
(->> steps
(reduce (fn [loc step]
(case step
-1 (-> loc
(zip/append-child [])
(zip/down)
(zip/rightmost))
0 (zip/up loc)
(zip/append-child loc step)))
(zip/vector-zip []))
(zip/root)))
(assert (= (deepen in) out))
Somehow this feels like cheating:
[(read-string
(clojure.string/join " "
(replace {-1 "[" 0 "]"}
[-1 1 2 -1 3 0 -1 2 -1 4 0 3 0 0])))]
This is not too hard with some recursion:
(defn numbers->tree [xs]
(letfn [(step [xs]
(loop [ret [], remainder xs]
(if (empty? remainder)
[ret remainder]
(let [x (first remainder)]
(case x
0 [ret (next remainder)]
-1 (let [[ret' remainder'] (step (next remainder))]
(recur (conj ret ret'), remainder'))
(recur (conj ret x) (next remainder)))))))]
(first (step xs))))
The idea is to have a function (step) that finds a sub-tree, and returns that tree as well as what numbers are left to be processed. It proceeds iteratively (via loop) for most inputs, and starts a recursive instance of itself when it runs into a -1. The only tricky part is making sure to use the remainder returned from these recursive invocations, rather than proceeding on with the list you were in the middle of.

what is the clojure way to do things

As part of a larger program, I'm testing a function that will turn a string of days on which a class occurs (such as "MWF") into a list of seven numbers: (1 0 1 0 1 0 0).
I first translate"TH" (Thursday) to "R" and "SU" (Sunday) to "N" to make things a bit easier.
I came up with the following code:
(defn days-number-maker
"Recursively compare first item in days of week with
first item in string of days. If matching, add a 1,
else add a zero to the result"
[all-days day-string result]
(if (empty? all-days) (reverse result)
(if (= (first all-days) (first day-string))
(recur (rest all-days)(rest day-string) (conj result 1))
(recur (rest all-days) day-string (conj result 0)))))
(defn days-to-numbers
"Change string like MTTH to (1 1 0 1 0 0 0)"
[day-string]
(let [days (clojure.string/replace
(clojure.string/replace day-string #"TH" "R") #"SU" "N")]
(days-number-maker "MTWRFSN" days (list))))
The good news: the code works. The bad news: I'm convinced I'm doing it wrong, in the moral purity sense of the word. Something inside of me says, "You could have just used (map...) to do this the right way," but I can't see how to do it with (map). So, my two questions are:
1) Is there such a thing as "the Clojure way," and if so,
2) How can I rewrite the code to be more Clojure-ish?
You can use map and sets
Using map and sets:
(defn days-number-maker
[all-days day-string]
(let [day-set (set day-string)]
(map (fn [day]
(if (day-set day)
1
0))
all-days)))
(defn days-to-numbers
"Change string like MTTH to (1 1 0 1 0 0 0)"
[day-string]
(let [days (clojure.string/replace
(clojure.string/replace day-string #"TH" "R") #"SU" "N")]
(days-number-maker "MTWRFSN" days)))
This is how I would do it a bit more succinctly:
(defn days-to-numbers
"Change string like MTTH to (1 1 0 1 0 0 0)"
[week-string]
(let [char-set (set (clojure.string/replace
(clojure.string/replace week-string "TH" "R") "SU" "N"))]
(map #(if (char-set %) 1 0)
"MTWRFSN")))
Tests:
=> (days-to-numbers "")
(0 0 0 0 0 0 0)
=> (days-to-numbers "MTWTHFSSU")
(1 1 1 1 1 1 1)
=> (days-to-numbers "MTHSU")
(1 0 0 1 0 0 1)
=> (days-to-numbers "FM")
(1 0 0 0 1 0 0)
Following on from #TheQuickBrownFox's answer ...
You don't need to recode "TH" and "SU": the second letters will
do.
Use false or nil instead of 0, so that you can apply logical tests directly.
Return the result as a vector, as you're quite likely to want to
index into it.
Giving ...
(defn days-to-numbers [ds]
(let [dns (->> ds
(partition-all 2 1)
(remove #{[\S \U] [\T \H]})
(map first)
set)]
(mapv dns "MTWHFSU")))
For example,
(days-to-numbers "MTTH")
;[\M \T nil \H nil nil nil]
Though the function is mis-named, as the elements are logical values, not numbers.
I'd prefer to return the set of day numbers:
(def day-index (into {} (map-indexed (fn [x y] [y x]) "MTWHFSU")))
;{\M 0, \T 1, \W 2, \H 3, \F 4, \S 5, \U 6}
(defn day-numbers [ds]
(->> ds
(partition-all 2 1)
(remove #{[\S \U] [\T \H]})
(map (comp day-index first))
set))
For example,
(day-numbers "MTTH")
;#{0 1 3}

How to move first list item to the end?

For given list:
(1 2 3 4)
I'd like to get as output:
(2 3 4 1)
Code I came up with looks like this:
(flatten (cons (rest l) (list (first l))))
However my feeling is, that I overcomplicated this. Any other ideas?
You don't need to flatten a cons, just use concat.
Here is an example:
(let [fruit ["apple" "orange" "grapes" "bananas"]]
(concat (rest fruit) [(first fruit)])
Developing #stonemetal's hint, we can quickly and lazily rotate a vector thus:
(defn rotate [v n]
(let [cv (count v), n (mod n cv)]
(concat (subvec v n cv) (subvec v 0 n))))
It works in either direction:
(map #(rotate (vec (range 5)) %) (range -2 8))
;((3 4 0 1 2)
; (4 0 1 2 3)
; (0 1 2 3 4)
; (1 2 3 4 0)
; (2 3 4 0 1)
; (3 4 0 1 2)
; ...
; (2 3 4 0 1))
So to rotate the first in a sequence to the end:
(rotate (vec (range 1 5)) 1)
You can also use destructuring (either on the function arguments or in a let binding).
(let [[h & tail] '(1 2 3 4)]
(concat tail (list h))) ;=> (1 2 3 4)

How to count the number of ones in a vector, given an upper limit

Given a vector of ones and zeros, I would like to count the number of entries with a value one. However, the vector may be very long and I only care to know if the vector has zero, one, or more entries with a value o f one.
Using the approach given here, I can count the number of ones in the vector.
(count (filter #{1} [1 0 1 0 0 1 1]))
Can I limit filter (or use some other approach) to avoid visiting any more than three elements of the vector, in this case?
Filter is lazy, so will only do as much work as required. Since you only care about having no 1's, one 1's or two or more ones, you only need to examine up to two elements of the filtered sequence of 1's, so just take 2 before you count:
user=> (count (take 2 (filter #{1} [1 0 1 0 0 1 1])))
2
user=> (count (take 2 (filter #{1} [0 0 0 0 0 0 0])))
0
user=> (count (take 2 (filter #{1} [0 0 0 0 0 0 1])))
1
user=> (def rare (repeatedly #(if (< (rand) 0.0001) 1 0)))
#'user/rare
user=> (take 10 rare)
(0 0 0 0 0 0 0 0 0 0)
user=> (count (take 2 (filter #{1} rare)))
2