How to compare elements of two lists in clojure

I want to compare two lists in clojure,

(def a '(1 2 3 4 5))
(def b '(3 2 7 8 10))

make the result (2 3) or (3 2) by comparing the elements from two lists.

(defn compareList[x, y]
  (do
   (def result '())
   (def i 0)
   (def j 0)
   (while (< i 5) 
     (while (< j 5)
       (if (= (nth x i) (nth y j)) 
         (def result (conj (nth x i) result)))
       (def j (inc j))
     )
   )
   result))


(print (compareList a b))

this is my code. but the result is (). where am i wrong help me please.

+4
source share
3 answers

Using the kit will be more suitable for your case.

(clojure.set/intersection #{1 2 3 4 5} #{3 2 7 8 10})

This will exit #{2 3}

+6
source

This seems like a list comprehension (using a macro forthat returns LazySeq):

(for [a '(1 2 3 4 5) b '(3 2 7 8 10)
  :when
  (= a b)]
  a)

;; => (2 3)
+1
source

@turing . .

, :

user=> (set '(1 1 2 3 2 4 5 5))
#{1 2 3 4 5}

, .

0

Source: https://habr.com/ru/post/1615217/


All Articles