;; a Clojure function has no "return": it gives back the value
;; of its last expression. An early exit is a function that stops
;; walking as soon as it has the answer — that is what "some" does
(defn contain-number? [numbers number]
(boolean (some #(= % number) numbers)))
(def data [1 2 3])
(def contain-2? (contain-number? data 2))
;; contain-2? is true
(def contain-4? (contain-number? data 4))
;; contain-4? is false
(println "contain-2? is" contain-2?)
(println "contain-4? is" contain-4?)
;; spelled out as a loop: returning a value means not calling recur
(defn contain-number-loop? [numbers number]
(loop [[x & more] numbers]
(cond
(nil? x) false
(= x number) true ; <- here the loop returns instead of repeating
:else (recur more))))
(println "loop version:" (contain-number-loop? data 2))