Created
July 8, 2020 10:08
-
-
Save gerritjvv/15c721c0e1eac18cad953120975d3968 to your computer and use it in GitHub Desktop.
Clojure - finding something in a list
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn in? [vs ls] | |
(boolean (some (partial (set vs)) ls))) | |
(in? [1 2] [3 4 2 5]) => false | |
(in? [1 2] [3 1 4 2 5]) => true | |
(defn in? [v ls] | |
(loop [[x & rs] ls] | |
(if-not x | |
false | |
(if (= x v) | |
true | |
(recur [rs]))))) | |
(in? 3 [2 3 4]) => true | |
(in? 5 [2 3 4]) => false | |
(defn in? [v ls] | |
(boolean (some #(= v %) ls))) | |
(in? 3 [2 3 4]) => true | |
(in? 5 [2 3 4]) => false | |
(defn in? [v ls] | |
(reduce (fn [_ x] | |
(if (= x v) | |
(reduced true) | |
false)) false ls)) | |
(in? 3 [2 3 4]) => true | |
(in? 5 [2 3 4]) => false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment