Fork me on GitHub
clojure beginner02:04:49

determine the largest 3 numbers in a vector (defn new_max3s [[max1 max2 max3] x] (cond (> x max1) [x max1 max2] (> x max2) [max1 x max2] (> x max3) [max1 max2 x] :else [max1 max2 max3])) (reduce new_max3s [Integer/MIN_VALUE Integer/MIN_VALUE Integer/MIN_VALUE] [11 80 10 4 5 6 1]) is it ok to create a new list in new_max3s everytime a large number is found


I believe a new vector gets created at each iteration, even if a larger number isn't found. Don't think it's gonna be a huge problem. Ephemeral garbage is pretty cheap on the JVM.


And I believe that because destructuring pulls out and binds max1, max2 and max3 to values and in the :else branch a new vector gets constructed. You could avoid that by explicitly binding the first arg like this:

(defn new_max3s [[max1 max2 max3 :as current_maxima] x]
    (> x max1) [x max1 max2]
    (> x max2) [max1 x max2]
    (> x max3) [max1 max2 x]
    :else current_maxima))
I'm sure there are better ways to write this fn though.

clojure beginner03:04:11

@jaihindh.reddy thanks. It looks like i cant recur at every cond predicate , So to avoid the list , i tried this - (defn -max3s [xs max1 max2 max3] (if (empty? xs) [max1 max2 max3] (let [x (first xs)] (cond (> x max1) (-max3s (rest xs) x max1 max2) (> x max2) (-max3s (rest xs) max1 x max2) (> x max3) (-max3s (rest xs) max1 max2 x) :else (-max3s (rest xs) max1 max2 max3))))) (defn max3s [xs] (-max3s xs Integer/MIN_VALUE Integer/MIN_VALUE Integer/MIN_VALUE))

clojure beginner03:04:58

i liked the reduce better, since it frees me from thinking about recursion etc and just worry about the function that i pass to reduce. That is just deal with the current iteration.

clojure beginner03:04:18

which seems like a simple problem to solve. But its not quite clear what is clojure "style"


is this an exercise where you're supposed to avoid using parts of the standard library?


otherwise the clearest way would be something like (take 3 (reverse (sort xs)))


or (take-last 3 (sort xs))

clojure beginner03:04:39

@qythium thanks. Yep your solution looks cool. Yes am laboring through Structure and Interpretation of Computer Programs and trying to solve the exercise in clojure


Ah, that makes sense


There's a lot of emphasis on recursion there which won't be idiomatic Clojure


eg. your -max3s function above might blow the call stack on a long list because there's no automatic tail-call optimization


@gayathrik Something to bear in mind about SICP is that ... hahaha, yeah, I was about to say that ... and there's also quite a bit of mutation in SICP as I recall?

Ivan Koz05:04:08

Is there a way to extract form into a defn in cursive?


You might wanna ask in #cursive

👍 4

More likely to get help quicker there

Ivan Koz05:04:26

oh i never knew we have so many channels on slack, wow haha


Everytime I change my code I need to re-run lein run and It take too much time


How can I fix that ?


still don't know how to use reloaded in exopaste project 😥

Ivan Koz05:04:17

must be what you need, also read related article by Stuart Sierra


@minhnhat10bk before you do all that, make sure you read the REPL guide at