Simple 2D array in Clojure - clojure

I'm still learning Clojure and can't seem to find a simple answer for this. I've seen similar questions answered with complex code specific for the OP's issue so please let me know the most accurate or acceptable version of the following:
int[][] arrayTest = new int[width][height];
...
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
int a = arrayTest[x][y];
if (a < 100) {
arrayTest[x][y] = 0;
}
}
}

The literal translation is straightforward:
(def array-test
(make-array Integer/TYPE width height))
(doseq [x (range width)
y (range height)]
(when (< (aget array-test x y) 100)
(aset-int array-test x y 0)))
Note, however, that arrays are not commonly used in Clojure. Unless you want to do fast computations or work with existing Java code, you shouldn't normally be creating arrays and other mutable data structures. Most likely, what you want to implement can be done with Clojure's persistent collections instead.

Represented as a sequence of sequences...
user=> (doc repeat)
-------------------------
clojure.core/repeat
([x] [n x])
Returns a lazy (infinite!, or length n if supplied) sequence of xs.
nil
user=> (def width 8)
#'user/width
user=> (def height 6)
#'user/height
user=> (repeat width 0)
(0 0 0 0 0 0 0 0)
user=> (pprint (repeat height (repeat width 0)))
((0 0 0 0 0 0 0 0)
(0 0 0 0 0 0 0 0)
(0 0 0 0 0 0 0 0)
(0 0 0 0 0 0 0 0)
(0 0 0 0 0 0 0 0)
(0 0 0 0 0 0 0 0))
nil

Related

Why so many 0s in the answer?

(take 100 (iterate rand-int 300))
evaluates differently, of course, each time... but usually with a ton of zeros. The result always leads with a 300. For example:
(300 93 59 58 25 14 9 4 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)
I would have expected 100 random integers between 0 and 300.
What am I not understanding?
See docs for iterate:
Returns a lazy sequence of x, (f x), (f (f x)) etc. f must be free of side-effects
So, that's the reason your sequence is always starting with 300.
And why there are so many zeros? When you use iterate like this, rand-int takes the previous result and uses it as a new upper limit (exclusive) for a random number. So, your results can look like this:
300
=> 300
(rand-int *1)
=> 174
(rand-int *1)
=> 124
(rand-int *1)
=> 29
(rand-int *1)
=> 17
(rand-int *1)
=> 16
(rand-int *1)
=> 7
...
You can check yourself that this sequence leads to zero.
If you really want to get 100 random integers between 0 and 300, use repeatedly instead:
(repeatedly 100 #(rand-int 300))

Casting Long to Integer inside function

I am writing a function that converts three numbers (r,g,b) into a single integer that is used by java.awt.Color
(defn to-rgb
([r g b] (bit-or
(long 4278190080) ; the alpha channel = (bit-shift-left 255 24)
(bit-shift-left r 16)
(bit-shift-left g 8)
b)))
Using the Color constructor and .getRGB
bitmap> (.getRGB (Color. 0 0 0))
-16777216
bitmap> (type (.getRGB (Color. 0 0 0)))
java.lang.Integer
(Integer/toBinaryString (.getRGB (Color. 0 0 0)))
"11111111000000000000000000000000"
And then my version. So I need to cast to an Integer
bitmap> (to-rgb 0 0 0)
4278190080
bitmap> (Integer. (to-rgb 0 0 0))
-16777216 ; as expected
So I decided to put the cast inside the function as I always want an Integer returned.
(defn to-rgb2
([r g b] (Integer. (bit-or
(long 4278190080)
(bit-shift-left r 16)
(bit-shift-left g 8)
b))))
bitmap> (to-rgb2 0 0 0)
IllegalArgumentException Value out of range for int: 4278190080 clojure.lang.RT.intCast (RT.java:1205)
In Java, an Integer is only 32 bits. Change it to Long:
(Long. ...)
The problem is the constant 4278190080 translates to 0xFF000000 in hex, where we see that the left-most bit is set. In Java, since integer values are signed 32-bit, the left-most bit cannot be set as it is reserved for negative numbers in two's-compliment notation.
java.lang.Integer.MAX_VALUE = 0x7FFFFFFF
in hex.
Update
If you need it as a 32-bit value, use Long.intValue() like so:
(defn to-rgb3
([r g b] (.intValue
(Long. (bit-or
(long 4278190080)
(bit-shift-left r 16)
(bit-shift-left g 8)
b)))))
(dotest
(spyxx (to-rgb3 0 0 0)))
(to-rgb3 0 0 0) => java.lang.Integer->-16777216

Appending list in lisp

In lisp, I am appending lists as:
(setq newlist (append (side a b)(this a b) (that a b) ))
This appends all the required list as: (1 0 0 0 2 0 4 0 6 0)
but what I want is something like this: ((1 0)(0 0)(2 0)(4 0)(6 0))
What should I do to get the required format. Please post code examples in lisp.
So in fact you just need to restructure the elements after you have appended it:
(loop :for (e1 e2)
:on '(1 0 0 0 2 0 4 0 6 0)
:by #'cddr
:collect (list e1 e2))
; ==> ((1 0) (0 0) (2 0) (4 0) (6 0))
Suggested reading is LOOP for black belts, the section you should pay attention to which I've used here is "Looping Over Collections and Packages" and "Destructuring Variables". This is probably the chapter from Practical Common Lisp I read the most. The whole book is very good so every lisper should know about it.

Generate truth table for n operators

I've been tasked with writing a function that generates a table given n operators. The truth table must be in a list and each row of the table must be in separate lists (inside the main list).
I know the solution involves recursion but I just can't seem to think it through.
Can someone help me out? This is only a small part of the assignment.
Easiest way I can think of off the top of my head is to simply convert 2^n to binary and count down, then convert the output to a list.
ie for n=3:
Truth table:
a b c
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1
2^3 = 8, 8 in binary = 1000, start from 1000-1 = 111 and work your way down to 0, record outputs, and voila!
If hkf's interpretation of your question is right, this should work in Racket:
#lang racket
(define (generate-table n)
(if (zero? n)
'(())
(for*/list ((y (in-list (generate-table (sub1 n))))
(x (in-list '(0 1))))
(cons x y))))
Use it like this:
(generate-table 3)
> ((0 0 0) (1 0 0) (0 1 0) (1 1 0) (0 0 1) (1 0 1) (0 1 1) (1 1 1))
Let's assume that all N operators are binary functions, like AND and OR.
;; Common Lisp
(defun truth-tables (ops)
(loop for op in ops
collecting
(loop for args in '((nil nil) (nil t) (t nil) (t t))
collecting (eval `(,op ,#args)))))
(truth-tables '(and or xor)) -> ((NIL NIL NIL T) (NIL T T T) (NIL T T NIL))
This gives you an idea. Well, here I don't have "each row of the truth table" as a sublist; I have the columns for the AND, OR and XOR truth tables, respectively. The input variable combinations are left implicit: you know that the third entry of every one corresponds to (<op> t nil). Your description of the problem is not very clear.
As you can also see, I cheated by using the Lisp operators through generated code which is dynamically evaluated.

help with substitution model [Sicp], using Clojure

I am studying the sicp book, and I have a doubt with the substitution model of a procedure:
(defn A
[x,y]
(cond (= y 0) 0
(= x 0) (* 2 y)
(= y 1) 2
:else (A (- x 1) (A x (- y 1)))))
This procedure is part of the exercise 1.10.
If I run the function in REPL with the following parameters (A 1 10), the result is 1024. I decided to verify the result using the Substitution Model, but the result was 2048.
This is the substitution model that I wrote. There is something wrong, but I don't know what.
(A 1 10)
(A (- 1 1) (A 1 (- 10 1))))
(A 0 (A 1 9)))
(A 0 (A (- 1 1) (A 1 (- 9 1)))))
(A 0 (A 0 (A 1 8))))
(A 0 (A 0 (A (- 1 1) (A 1 (- 8 1))))))
(A 0 (A 0 (A 0 (A 1 7)))))
(A 0 (A 0 (A 0 (A (- 1 1) (A 1 (- 7 1)))))))
(A 0 (A 0 (A 0 (A 0 (A 1 6))))))
(A 0 (A 0 (A 0 (A 0 (A (-1 1) (A 1 (- 6 1))))))))
(A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 1 5))))))))
(A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A (-1 1) (A 1 (- 5 1))))))))))
(A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 1 4)))))))))
(A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A (-1 1) (A 1 (- 4 1)))))))))))
(A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 1 3))))))))))
(A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A (-1 1) (A 1 (-3 1))))))))))
(A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 1 2)))))))))
(A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A (-1 1) (A 1 (- 2 1))))))))))
(A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 1 1))))))))))
(A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (* 2 1))))))))))
(A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 2)))))))))
(A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (* 2 2)))))))))
(A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 4))))))))
(A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (* 2 4))))))))
(A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 8)))))))
(A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (* 2 8)))))))
(A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 16))))))
(A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (* 2 16)))))))
(A 0 (A 0 (A 0 (A 0 (A 0 (A 0 32))))))
(A 0 (A 0 (A 0 (A 0 (A 0 (* 2 32))))))
(A 0 (A 0 (A 0 (A 0 (A 0 64)))))
(A 0 (A 0 (A 0 (A 0 (* 2 64)))))
(A 0 (A 0 (A 0 (A 0 128))))
(A 0 (A 0 (A 0 (* 2 128))))
(A 0 (A 0 (A 0 256)))
(A 0 (A 0 (* 2 256)))
(A 0 (A 0 512))
(A 0 (* 2 512))
(A 0 1024)
2048 ????
Can anyone indicate what I did wrong?
I am sorry for the length of the question.
Consider these lines:
(A 0 (A 0 (A 0 (A 1 7)))))
(A 0 (A 0 (A 0 (A (- 1 1) (A 1 (- 7 1)))))))
(A 0 (A 0 (A 0 (A 0 (A 1 6))))))
(A 0 (A 0 (A 0 (A 0 (A (-1 1) (A 1 (- 6 1))))))))
(A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 1 5))))))))
Strip off the redundant outer layers:
(A 1 7))
(A (- 1 1) (A 1 (- 7 1))))
(A 0 (A 1 6)))
(A 0 (A (-1 1) (A 1 (- 6 1)))))
(A 0 (A 0 (A 0 (A 1 5)))))
Somewhere in here you've ended up with mismatched parentheses, but that's not important. Note that in going from A 1 7 to A 1 6, a single outer layer of A 0 _ is created, as expected. In going from A 1 6 to A 1 5, you've got two new layers of A 0 _. Each of these ends up doubling the result, so that's why your answer is off by a factor of 2.