Hello, Not a lot of time to answer, so I'll be straight to the point below :
On 22 déc, 16:23, Piotr 'Qertoip' Włodarek <qert...@gmail.com> wrote: > Hello, > > Being new to Clojure, to Lisp and to functional programming in > general, I have some trouble wraping my head around it. > > As the first exercice, I would like to print multiplication table of > specified order, like: > (print-multiplication-table 3) > 1 2 3 > 2 4 6 > 3 6 9 > > I came that far: > > (defn multiplication-row [n k] > (map (partial * k) (range 1 (inc n)))) > > (defn multiplication-table [n] > (map (partial multiplication-row n) (range 1 (inc n)))) > > (println (multiplication-table 3)) ; => ((1 2 3) (2 4 6) (3 6 9)) > > Now, how to pretty print this? > > This does not work - prints nothing - why?: > (defn pretty-print-row [row] > (map print row)) The objective of map is not initially to go through the list in order to have side effects (as pretty print would be) : it is to return a new sequence with new computed values from initial values. So map has been made, by design, lazy. To make you example work, you need to wrap the call to map with a call to dorun which will force the sequence from beginning to end, then forcing your side effect printing. See here ( http://clojure.org/api#toc203 ) for more detail on doseq. > This also does not work - throws > java.lang.UnsupportedOperationException: Can only recur from tail > position (hello_world.clj:47) - why?: > > (defn pretty-print-row [row] > (if (first row) > ((print (first row)) > (recur (rest row))))) > > Once I remove print expression, exception is not thrown (what the > heck?) You have a bug : you wrote extra enclosing parens around print & recur -> the right syntax for if is : (if (first row) (print (first row)) (recur (rest row))) HTH, -- Laurent > > Regards, > Piotrek --~--~---------~--~----~------------~-------~--~----~ 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 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 -~----------~----~----~----~------~----~------~--~---