Fork me on GitHub
#beginners
<
2015-12-26
>
kopasetik08:12:53

Why am I getting an error here?

sllyq08:12:31

Im a little bit new to clojure myself, but using (nth vector2 %1) instead of (vector2 %1) fixes it

sllyq08:12:43

in dot-product function it is

sllyq09:12:01

also I think this is the better way to write it

sllyq09:12:16

(defn dot-product [vector1 vector2]
  (apply + (map #(* %1 %2) vector1 vector2))
  )

kopasetik09:12:59

@sllyq: OK, thanks. Just a little confused about how apply works in Clojure… so used to JS apply

sllyq09:12:43

(apply func array) is equivalent to js func.apply(array) I think

sllyq09:12:22

or func(...array)

sllyq09:12:51

it should be func.apply(null, array) in first example

sllyq09:12:34

(apply + [1 2 3]) just basically makes it (+ 1 2 3)

kopasetik09:12:01

ok, but i could also use reduce instead, right?

sllyq09:12:25

It obiously does the same as your function, just threw it out there because it makes more sense to me this way. You can write whichever way you want it simple_smile

kopasetik17:12:53

@sllyq: Thanks for the insight! simple_smile

kopasetik17:12:04

OK, I’m a bit confused as to how to compare the results of 2 rounds of a loop

sllyq17:12:58

I used to do it like this

sllyq17:12:08

(loop [prev-val nil]
  (let [curr-val (+ 1 2)]
    (if (nil? prev-val)
      (recur curr-val)
      (compare-and-do-something-here))))

sllyq17:12:19

There might be a better way, not sure