
Hello everyone, Just trying to understand the sequence function as follows: sequence [Just 1] -- evaluates to Just [1] sequence = foldr mcons (return []) where mcons p q = p >>= \x -> q >>= \y -> return (x:y) -- I'm trying to walk through the code as follows, I understand what is below isn't -- haskell code p >>= \x -> [] q >>= \y -> Just 1 return (x:y) -- [] : Just 1 Am I thinking of sequence correctly here? Best regards, Jim

Remember that foldr has flipped operator order from foldl.
:t foldl foldl :: Foldable t => (b -> a -> b) -> b -> t a -> b :t foldr foldr :: Foldable t => (a -> b -> b) -> b -> t a -> b
That means that you should expand them in the opposite order from how
it seems to read.
p >>= \x -> -- Just 1 >>= \ 1
q >>= \y -> -- return [] >>= \ []
return (1 : []) -- Just [1]
On Tue, Sep 26, 2017 at 12:59 PM, Jimbo
Hello everyone,
Just trying to understand the sequence function as follows:
sequence [Just 1]
-- evaluates to Just [1]
sequence = foldr mcons (return []) where mcons p q = p >>= \x -> q >>= \y -> return (x:y)
-- I'm trying to walk through the code as follows, I understand what is below isn't -- haskell code
p >>= \x -> [] q >>= \y -> Just 1 return (x:y) -- [] : Just 1
Am I thinking of sequence correctly here?
Best regards,
Jim
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

Thank you very much. Final question, in the line: return (1 : []) -- Just [1] Does the value ([1] in this case) get wrapped in Just because of the type signature of sequence? I.e sequence :: Monad m => [m a] -> m [a] On 26/09/2017 1:49 PM, David McBride wrote:
Remember that foldr has flipped operator order from foldl.
:t foldl foldl :: Foldable t => (b -> a -> b) -> b -> t a -> b :t foldr foldr :: Foldable t => (a -> b -> b) -> b -> t a -> b
That means that you should expand them in the opposite order from how it seems to read.
p >>= \x -> -- Just 1 >>= \ 1 q >>= \y -> -- return [] >>= \ [] return (1 : []) -- Just [1]
On Tue, Sep 26, 2017 at 12:59 PM, Jimbo
wrote: Hello everyone,
Just trying to understand the sequence function as follows:
sequence [Just 1]
-- evaluates to Just [1]
sequence = foldr mcons (return []) where mcons p q = p >>= \x -> q >>= \y -> return (x:y)
-- I'm trying to walk through the code as follows, I understand what is below isn't -- haskell code
p >>= \x -> [] q >>= \y -> Just 1 return (x:y) -- [] : Just 1
Am I thinking of sequence correctly here?
Best regards,
Jim
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

Monadic bind has this signature (Monad m => m a -> (a -> m b) -> m b.
Note that m is the same in both arguments and also the return value.
So when you see p >>= \_ -> q ..., that means both p and q must be (m
Something), where the m is the same.
So when you go (Just 1 >>= \x -> return [] >>= \y -> return (x:y))
you know that return [] and return (x:y) are both using the Maybe
Monad instance because in Just 1, the m is Maybe.
So return [] is then equivalent to Just [], and return (x:y) is
equivalent to Just (x:y). I hope that made sense.
On Tue, Sep 26, 2017 at 2:10 PM, Jimbo
Thank you very much. Final question, in the line:
return (1 : []) -- Just [1]
Does the value ([1] in this case) get wrapped in Just because of the type signature of sequence? I.e
sequence :: Monad m => [m a] -> m [a]
On 26/09/2017 1:49 PM, David McBride wrote:
Remember that foldr has flipped operator order from foldl.
:t foldl
foldl :: Foldable t => (b -> a -> b) -> b -> t a -> b
:t foldr
foldr :: Foldable t => (a -> b -> b) -> b -> t a -> b
That means that you should expand them in the opposite order from how it seems to read.
p >>= \x -> -- Just 1 >>= \ 1 q >>= \y -> -- return [] >>= \ [] return (1 : []) -- Just [1]
On Tue, Sep 26, 2017 at 12:59 PM, Jimbo
wrote: Hello everyone,
Just trying to understand the sequence function as follows:
sequence [Just 1]
-- evaluates to Just [1]
sequence = foldr mcons (return []) where mcons p q = p >>= \x -> q >>= \y -> return (x:y)
-- I'm trying to walk through the code as follows, I understand what is below isn't -- haskell code
p >>= \x -> [] q >>= \y -> Just 1 return (x:y) -- [] : Just 1
Am I thinking of sequence correctly here?
Best regards,
Jim
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

"So when you go (Just 1 >>= \x -> return [] >>= \y -> return (x:y)) you know that return [] and return (x:y) are both using the Maybe Monad instance because in Just 1, the m is Maybe." I understand what you are saying but just to further clarify for my own understanding, is this what is meant by Haskell being able to reason about your program? That is, because you have "chosen" m to be the Maybe monad by using Just 1 (quoted above), the following return functions (in the above computation) use the definition for return below with m being Maybe? class Monad mwhere (>>=) :: m a-> (a-> m b) -> m b return :: a-> m a On 26/09/2017 2:28 PM, David McBride wrote:
Monadic bind has this signature (Monad m => m a -> (a -> m b) -> m b. Note that m is the same in both arguments and also the return value.
So when you see p >>= \_ -> q ..., that means both p and q must be (m Something), where the m is the same.
So when you go (Just 1 >>= \x -> return [] >>= \y -> return (x:y)) you know that return [] and return (x:y) are both using the Maybe Monad instance because in Just 1, the m is Maybe.
So return [] is then equivalent to Just [], and return (x:y) is equivalent to Just (x:y). I hope that made sense.
On Tue, Sep 26, 2017 at 2:10 PM, Jimbo
wrote: Thank you very much. Final question, in the line:
return (1 : []) -- Just [1]
Does the value ([1] in this case) get wrapped in Just because of the type signature of sequence? I.e
sequence :: Monad m => [m a] -> m [a]
On 26/09/2017 1:49 PM, David McBride wrote:
Remember that foldr has flipped operator order from foldl.
:t foldl foldl :: Foldable t => (b -> a -> b) -> b -> t a -> b :t foldr foldr :: Foldable t => (a -> b -> b) -> b -> t a -> b
That means that you should expand them in the opposite order from how it seems to read.
p >>= \x -> -- Just 1 >>= \ 1 q >>= \y -> -- return [] >>= \ [] return (1 : []) -- Just [1]
On Tue, Sep 26, 2017 at 12:59 PM, Jimbo
wrote: Hello everyone,
Just trying to understand the sequence function as follows:
sequence [Just 1]
-- evaluates to Just [1]
sequence = foldr mcons (return []) where mcons p q = p >>= \x -> q >>= \y -> return (x:y)
-- I'm trying to walk through the code as follows, I understand what is below isn't -- haskell code
p >>= \x -> [] q >>= \y -> Just 1 return (x:y) -- [] : Just 1
Am I thinking of sequence correctly here?
Best regards,
Jim
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

This is known as type inference.
On Sep 26, 2017 17:41, "Jimbo"
"So when you go (Just 1 >>= \x -> return [] >>= \y -> return (x:y)) you know that return [] and return (x:y) are both using the Maybe Monad instance because in Just 1, the m is Maybe."
I understand what you are saying but just to further clarify for my own understanding, is this what is meant by Haskell being able to reason about your program? That is, because you have "chosen" m to be the Maybe monad by using Just 1 (quoted above), the following return functions (in the above computation) use the definition for return below with m being Maybe? class Monad m where (>>=) :: m a -> (a -> m b) -> m b return :: a -> m a
On 26/09/2017 2:28 PM, David McBride wrote:
Monadic bind has this signature (Monad m => m a -> (a -> m b) -> m b. Note that m is the same in both arguments and also the return value.
So when you see p >>= \_ -> q ..., that means both p and q must be (m Something), where the m is the same.
So when you go (Just 1 >>= \x -> return [] >>= \y -> return (x:y)) you know that return [] and return (x:y) are both using the Maybe Monad instance because in Just 1, the m is Maybe.
So return [] is then equivalent to Just [], and return (x:y) is equivalent to Just (x:y). I hope that made sense.
On Tue, Sep 26, 2017 at 2:10 PM, Jimbo
wrote: Thank you very much. Final question, in the line:
return (1 : []) -- Just [1]
Does the value ([1] in this case) get wrapped in Just because of the type signature of sequence? I.e
sequence :: Monad m => [m a] -> m [a]
On 26/09/2017 1:49 PM, David McBride wrote:
Remember that foldr has flipped operator order from foldl.
:t foldl
foldl :: Foldable t => (b -> a -> b) -> b -> t a -> b
:t foldr
foldr :: Foldable t => (a -> b -> b) -> b -> t a -> b
That means that you should expand them in the opposite order from how it seems to read.
p >>= \x -> -- Just 1 >>= \ 1 q >>= \y -> -- return [] >>= \ [] return (1 : []) -- Just [1]
On Tue, Sep 26, 2017 at 12:59 PM, Jimbo
wrote: Hello everyone,
Just trying to understand the sequence function as follows:
sequence [Just 1]
-- evaluates to Just [1]
sequence = foldr mcons (return []) where mcons p q = p >>= \x -> q >>= \y -> return (x:y)
-- I'm trying to walk through the code as follows, I understand what is below isn't -- haskell code
p >>= \x -> [] q >>= \y -> Just 1 return (x:y) -- [] : Just 1
Am I thinking of sequence correctly here?
Best regards,
Jim
_______________________________________________ Beginners mailing listBeginners@haskell.orghttp://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________ Beginners mailing listBeginners@haskell.orghttp://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________ Beginners mailing listBeginners@haskell.orghttp://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________ Beginners mailing listBeginners@haskell.orghttp://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
participants (2)
-
David McBride
-
Jimbo