(defn mult_first [a b] (* (first a) (first b)))</code> (defn mult_rest [a b] (if (= (count a) 1) (mult_first a b) (+ (mult_first a b) (mult_rest (rest a) (rest b))))) (defn dot [a b] (mult_rest a b)) (dot [2 3 4] [2 3 4]) 29
It probably could be a lot more elegant and I’m probably breaking who-knows-how-many conventions and guidelines, but it’s still the first clojure program I ever wrote (can be used to calculate dot product of two arbitrarily long vectors, as long as they are equally long).
Turns out I was right, the dot can be written in much simpler and cleaner way:
(defn dot "Returns the value of dot product of the vectors v1 and v2" [v1 v2] (reduce + (map * v1 v2)))
(thanks Minilight in Clojure: Vectors)