beginners

Getting started with Clojure/ClojureScript? Welcome! Also try: https://ask.clojure.org. Check out resources at https://gist.github.com/yogthos/be323be0361c589570a6da4ccc85f58f.
Chinmay Dalal 2020-12-05T17:40:28.013300Z

can bit-xor be used as logical xor?

Chinmay Dalal 2020-12-06T09:44:27.019500Z

oh yeah i expect only true or false as input

tws 2020-12-06T14:31:28.022300Z

i would use bit-xor as a guide to write your own multi-arity one

(defn bit-xor
  "Bitwise exclusive or"
  {:inline (nary-inline 'xor)
   :inline-arities >1?
   :added "1.0"}
  ([x y] (. clojure.lang.Numbers xor x y))
  ([x y & more]
    (reduce1 bit-xor (bit-xor x y) more)))

Chinmay Dalal 2020-12-07T13:48:52.078900Z

thanks !!

R.A. Porter 2020-12-05T17:41:43.013400Z

Nope.

Chinmay Dalal 2020-12-05T17:42:52.013600Z

oh

Chinmay Dalal 2020-12-05T17:43:08.013800Z

is there something elegant then?

seancorfield 2020-12-05T18:30:14.014Z

@chinmay.dalal.2201200 It's easy enough to write your own:

user=> (defn xor [p q] (and (or p q) (not (and p q))))
#'user/xor
user=> (for [a [true false] b [true false]] (list a b (xor a b)))
((true true false) (true false true) (false true true) (false false false))
user=>
See https://en.wikipedia.org/wiki/Exclusive_or#Equivalences,_elimination,_and_introduction for the logic.

Chinmay Dalal 2020-12-05T19:56:45.014400Z

thanks,

Chinmay Dalal 2020-12-05T19:57:16.014600Z

(defn xor [& args]
  (odd? (count (filter true? args))))

seancorfield 2020-12-05T20:51:00.014900Z

That's not quite the same semantics as the version I provided. Mine works with truthy/falsey which is more like the built-in and and or. Yours will only work with true for truthy.

seancorfield 2020-12-05T20:52:20.015100Z

If you update yours to (filter identity args) it'll have truthy/falsey semantics. (mine was strictly a 2-arity version so I can see why that's not generally useful!)

shidima 2020-12-05T21:09:54.016300Z

Could some one tell me why the calculate-row function returns nil and not a number? https://pastebin.com/TJzf7pXb (Advent of Code spoiler)

dpsutton 2020-12-05T21:21:16.017700Z

the first if is ignored. it is executed but nothing uses its return value so it might as well never happen. you then have a cond which will return a value if the predicate is hit, but if none of those predicates match it returns nil. also, you have a loop expression but there are no recur expressions so it will never loop

dpsutton 2020-12-05T21:22:01.017800Z

oh sorry. you do have recurs. but if neither condition of the cond is satisfied it returns nil

phronmophobic 2020-12-05T21:23:11.018Z

it looks like a misplaced paren. moving the cond statement to the else branch of the if statement fixes "returning nil" (I didn't check logical correctness otherwise):

(defn calculate-row
  [data rows]
  (loop [d data
         r rows]
    (if (= (rest d) ())
      r
      (cond
        (= (first d) \F) (recur (rest d) (bottom r))
        (= (first d) \B) (recur (rest d) (top r))))))

phronmophobic 2020-12-05T21:25:06.018200Z

I recommend the following idiom when looping using loop (which was originally recommend by someone in this slack): https://clojuredocs.org/clojure.core/loop#example-5a1de497e4b0a08026c48cc4

;; Iterating over a collection using loop

;; 1. First call (seq xs) on the given argument and then check for nil 
;; 2. Then call next/first and use these.

(loop [xs (seq [1 2 3 4 5])
       result []]
  (if xs
    (let [x (first xs)]
      (recur (next xs) (conj result (* x x))))
    result))

;; the same loop can be written using destructing,
;; but the compiler will generate two consecutive
;; seq calls and is slightly less efficient.

(loop [[x & r :as xs] (seq [])
       result []]
  (if xs
    (recur r (conj result (* x x)))
    result))

shidima 2020-12-05T21:28:44.018400Z

Yes, it indeed returns the value now. thanks!

shidima 2020-12-05T21:28:48.018600Z

I will keep that in mind