Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to compare elements of two lists in clojure

Tags:

list

clojure

I want to compare two list in clojure,

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

make a result (2 3) or (3 2) by comparing elements of 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))

it is my code. but result is (). where i mistake? help please.

like image 896
Sunok Jang Avatar asked Nov 10 '15 10:11

Sunok Jang


1 Answers

Using a set would be more appropriate for your case

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

That will output #{2 3}

like image 183
turingcomplete Avatar answered Oct 23 '22 21:10

turingcomplete