2012/6/12 Andy Coolware <andy.coolw...@gmail.com > > Hi, > > First a quick disclaimer. Those are my first steps in Clojure so I am > not be super accustomed to the language entire landscape and might > miss some basics here. However I was able to solve my first 4clojure > hard problem https://www.4clojure.com/problem/53 and have some second > thoughts after looking up top contributor's solutions as well as mine. > Why it has to be so complicated??? > > Conceptually, you just reduce the list to list of lists using a simple > condition < . Then you filter items longer then 1. And at the same > time you reduce the output to a first longest list. In this case, > stack for recursion is really not required, although I did use it in > my solution since I could figure out the "reduction" based way to > partition the source sequence. > > It also seems that imperative solution would be quite straightforward > although maintaining at least 4 state variables is not compelling at > all. Bottom line, I want to have a idiomatic Clojure solution ... Any > insight ....
Hello, It seems to me that it's not that bad to not overengineer too much for this problem : a single pass reduce, with an "accumulator" holding just 2 "states", the current chain, and the longest chain seen since the reduce started. And then when you've got the final result (which as stated will be an accumulator of the start of the last seen chain, and the longest chain ever seen), just pick into it to return the longest chain: #(letfn [(acc [[current longest] e] (if (= (dec e) (peek current)) (let [current (conj current e)] [current (if (> (count current) (count longest)) current longest)]) [[e] longest]))] (get (reduce acc [[] []] %) 1)) Straight to the point, isn't it ? Cheers, -- Laurent > > > Thx, > Andy > > -- > You received this message because you are subscribed to the Google > Groups "Clojure" group. > To post to this group, send email to clojure@googlegroups.com > Note that posts from new members are moderated - please be patient with your > first post. > To unsubscribe from this group, send email to > clojure+unsubscr...@googlegroups.com > For more options, visit this group at > http://groups.google.com/group/clojure?hl=en -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en