Thanks for the links. No, I am not on slack. On Friday, April 15, 2016 at 12:24:23 AM UTC-7, Colin Yates wrote: > > Hi Varun - the best advice I think I could give you is to spend a > whole bunch of time on https://clojuredocs.org and https://www.conj.io > (any others?) familiarising yourself with the core API. There is also > the official http://clojure.org/api/api but I find the example on > clojuredocs invaluable. > > For me, my first few months was spent writing a whole bunch of > unidiomatic and ugly code badly replicating what was already in the > core libs. In fact, there was a bit of a bad smell of writing lots of > Clojure code as the core lib provides a _lot_ more than you might > think (I came from Java). > > Also, you might want to invest in either core.typed or prismatic > schema for validating shapes of data. I think Brian Marick of Midje > fame has a similar answer but I can't recall the name. > > Oh, and this community is great - keep asking questions and someone > will answer :-) - are you on slack? > > On 15 April 2016 at 03:42, Varun Kamra <varun....@gmail.com <javascript:>> > wrote: > > Oh, I didn't even know we could loop using dotimes. Thanks for the > > explanation and for the code as well. > > > > > > On Thursday, April 14, 2016 at 6:58:57 PM UTC-7, Bobby Eickhoff wrote: > >> > >> The result of the last expression evaluated is always returned. Hence, > >> the shape of the function is what determines the points of return and > the > >> returned values. For example, if you're entire function is defined as > one > >> (if ...) statement, there are two possible points of return, each of > the > >> branches. > >> > >> But I think you're probably asking if you can return early from a > >> function, like in Java or Javascript. No, there's no return > "statement" in > >> Clojure. You just have to structure your functions a certain way. > >> > >> Here's an alternate version of your pyramid function which uses (recur > >> ...). This is equivalent to calling pyramid recursively, but the > compiler > >> is able to optimize this recursive call away. In other words, it will > never > >> overflow the stack. > >> > >> (defn pyramid [n] > >> (when (pos? n) > >> (dotimes [_ n] > >> (print "* ")) > >> (println) > >> (recur (dec n)))) > >> > >> > >> > >> > >> On Thursday, April 14, 2016 at 9:23:58 PM UTC-4, Varun Kamra wrote: > >>> > >>> On Thursday, April 14, 2016 at 5:58:33 PM UTC-7, Varun Kamra wrote: > >>> > Hey guys I am new to clojure. I was just experimenting by printing a > >>> > pyramid of stars 5 rows and 5 column. Here's the code: > >>> > > >>> > (defn pyramid [j i] > >>> > (if (and (= i 0) (neg? > >>> > (println "There's your pyramid")) > >>> > (if (= j 0) > >>> > (do (println) > >>> > (pyramid (- i 1) (- i 1))) > >>> > (do (print "* ") > >>> > (pyramid (- j 1) i)))) > >>> > > >>> > It's working fine till it prints the pyramid, but after printing it, > it > >>> > continues printing a lot of stars and eventually fail with stack > overflow. I > >>> > am guessing that a if I put a negative check I can prevent it but I > wanted > >>> > to know if there's a way to return from the recursive call instead. > >>> > >>> I understood now, it was going in the else condition of the if so > here'so > >>> the modified code > >>> > >>> (defn pyramid [j i] > >>> (if (= i 0) > >>> (println "There's your pyramid")) > >>> (if (= j 0) > >>> (do (println) > >>> (pyramid (- i 1) (- i 1)))) > >>> (if (not (neg? j)) > >>> (do (print "* ") > >>> (pyramid (- j 1) i)))) > >>> > >>> But my question still stands if ther's a way to return from function. > > > > -- > > You received this message because you are subscribed to the Google > > Groups "Clojure" group. > > To post to this group, send email to clo...@googlegroups.com > <javascript:> > > Note that posts from new members are moderated - please be patient with > your > > first post. > > To unsubscribe from this group, send email to > > clojure+u...@googlegroups.com <javascript:> > > 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 unsubscribe from this group and stop receiving emails from it, send > an > > email to clojure+u...@googlegroups.com <javascript:>. > > For more options, visit https://groups.google.com/d/optout. >
-- 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 unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.