On Tue, Nov 2, 2010 at 7:58 AM, Konrad Hinsen <konrad.hin...@fastmail.net>wrote:
> On 02.11.2010, at 11:45, Sunil S Nandihalli wrote: > > > following is the extract from the monads example ... > > It looks quite modified and no longer returns pairs! Here is the original: > > (with-monad sequence-m > (defn pairs [xs] > ((m-lift 2 #(list %1 %2)) xs xs))) > > > ; Another way to define pairs is through the m-seq operation. It takes > > ; a sequence of monadic values and returns a monadic value containing > > ; the sequence of the underlying values, obtained from chaining together > > ; from left to right the monadic values in the sequence. > > (with-monad sequence-m > > (defn pairs [xs] > > (m-seq (list xs xs)))) > > > > can somebody help me understand what is happening in the second one .. ? > > when would it be more appropriate to use m-seq instead of m-lift .. > > In most real-life cases you wouldn't have that choice, as m-lift and m-seq > do very different things. It's just for making pairs that either one is > fine. > > One example for the use of m-seq is given right after the pairs example: > > (with-monad sequence-m > (defn ntuples [n xs] > (m-seq (replicate n xs)))) > > This is a generalization from pairs to n-tuples. You couldn't do this using > m-lift applied to the list function because m-lift requires a fixed arity. This wouldn't work?: (with-monad sequence-m (defn ntuples [n xs] (apply (m-lift n list) (replicate n xs)))) (If m-lift is a macro that requires the arity arg to be known at macroexpansion time: (with-monad sequence-m (defn ntuples [n xs] (apply (eval `(m-lift ~n list)) (replicate n xs)))) instead. Icky, mind you.) -- 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