Re: monads m-seq and m-lift

2010-11-02 Thread nicolas.o...@gmail.com
As I understand it, m-seq is transforms a list of monadic computation
into a computation returning the list of result.
The resulting computation just sequentially does every computationin
the sequence and keep the result.

It is useful when the arity is not known at runtime foir example.


On Tue, Nov 2, 2010 at 10:45 AM, Sunil S Nandihalli
sunil.nandiha...@gmail.com wrote:
 Hello everybody,
  I was looking at the functions/macros defined with them I find that m-lift
 is very easy and intuitive ..
 following is the extract from the monads example ...

 (with-monad sequence-m
    (defn pairs [xs]
      ((m-lift 4 #(list :a %1 :b %2 :c %3 :d %4))
       (range 0 3)
       (range 10 13)
       (range 100 103)
       (range 1000 1003
 (pairs (range 2))
 ; 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 ..
 Thanks,
 Sunil.

 --
 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



-- 
Sent from an IBM Model M, 15 August 1989.

-- 
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


Re: monads m-seq and m-lift

2010-11-02 Thread Konrad Hinsen
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.

Another example is given later in the example collection under random number 
generators.

Konrad.

-- 
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


Re: monads m-seq and m-lift

2010-11-02 Thread Ken Wesson
On Tue, Nov 2, 2010 at 7:58 AM, Konrad Hinsen konrad.hin...@fastmail.netwrote:

 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

Re: monads m-seq and m-lift

2010-11-02 Thread Konrad Hinsen
On 02.11.2010, at 13:34, Ken Wesson wrote:

 This wouldn't work?:
 
 (with-monad sequence-m
   (defn ntuples [n xs]
  (apply (m-lift n list) (replicate n xs

No.

 (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.)

That should work. But once you are at that level of Lisp proficiency, you 
should also have heard the eval is evil lesson a few times ;-)

Konrad.

-- 
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


Re: monads m-seq and m-lift

2010-11-02 Thread Ken Wesson
On Tue, Nov 2, 2010 at 8:55 AM, Konrad Hinsen konrad.hin...@fastmail.netwrote:

 On 02.11.2010, at 13:34, Ken Wesson wrote:

  This wouldn't work?:
 
  (with-monad sequence-m
(defn ntuples [n xs]
   (apply (m-lift n list) (replicate n xs

 No.

  (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.)

 That should work. But once you are at that level of Lisp proficiency, you
 should also have heard the eval is evil lesson a few times ;-)


Yeah, I wouldn't actually use it for something like that in production code.
In this case I'd use m-seq. In fact the only time I've used eval in
production code, thus far, was in a system that had to generate new
functions on the fly based on data only available at runtime, and for
efficiency reasons those needed to become bytecode and potentially subject
to JIT compilation. So there was a compiler function that took some data,
constructed (fn ...) forms and eval'd them, and returned the resulting
function objects to its caller.

-- 
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

Re: monads m-seq and m-lift

2010-11-02 Thread Sunil S Nandihalli
Hi Konrad, nicolas and Ken,

Thanks for help. I am sure I will need more help when I try to walk through
the rest of the examples.

But the idea of monads is really neat..:)..


Sunil.

On Tue, Nov 2, 2010 at 7:19 PM, Ken Wesson kwess...@gmail.com wrote:

 On Tue, Nov 2, 2010 at 8:55 AM, Konrad Hinsen 
 konrad.hin...@fastmail.netwrote:

 On 02.11.2010, at 13:34, Ken Wesson wrote:

  This wouldn't work?:
 
  (with-monad sequence-m
(defn ntuples [n xs]
   (apply (m-lift n list) (replicate n xs

 No.

  (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.)

 That should work. But once you are at that level of Lisp proficiency, you
 should also have heard the eval is evil lesson a few times ;-)


 Yeah, I wouldn't actually use it for something like that in production
 code. In this case I'd use m-seq. In fact the only time I've used eval in
 production code, thus far, was in a system that had to generate new
 functions on the fly based on data only available at runtime, and for
 efficiency reasons those needed to become bytecode and potentially subject
 to JIT compilation. So there was a compiler function that took some data,
 constructed (fn ...) forms and eval'd them, and returned the resulting
 function objects to its caller.

  --
 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.comclojure%2bunsubscr...@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

Re: monads m-seq and m-lift

2010-11-02 Thread Sunil S Nandihalli
yea .. I was playing around and modified it in the due coarse.. :)
Sunil.

On Tue, Nov 2, 2010 at 5:28 PM, Konrad Hinsen konrad.hin...@fastmail.netwrote:

 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.

 Another example is given later in the example collection under random
 number generators.

 Konrad.

 --
 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.comclojure%2bunsubscr...@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