Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. Re: Sequence function (Jimbo)
2. Re: Sequence function (David McBride)
----------------------------------------------------------------------
Message: 1
Date: Tue, 26 Sep 2017 17:39:21 -0400
From: Jimbo <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] Sequence function
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"; Format="flowed"
"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 <[email protected]> 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 <[email protected]> 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
>>>> [email protected]
>>>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>>> _______________________________________________
>>> Beginners mailing list
>>> [email protected]
>>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>>
>> _______________________________________________
>> Beginners mailing list
>> [email protected]
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://mail.haskell.org/pipermail/beginners/attachments/20170926/53f87453/attachment-0001.html>
------------------------------
Message: 2
Date: Tue, 26 Sep 2017 19:55:36 -0400
From: David McBride <[email protected]>
To: Haskell Beginners <[email protected]>
Subject: Re: [Haskell-beginners] Sequence function
Message-ID:
<can+tr417lqcp2krawmga05f77emyprtgjig9v0tbiuaxrs8...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
This is known as type inference.
On Sep 26, 2017 17:41, "Jimbo" <[email protected]> wrote:
> "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 <[email protected]>
> <[email protected]> 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 <[email protected]>
> <[email protected]> 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
> [email protected]http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
>
> _______________________________________________
> Beginners mailing
> [email protected]http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
>
>
> _______________________________________________
> Beginners mailing
> [email protected]http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
> _______________________________________________
> Beginners mailing
> [email protected]http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://mail.haskell.org/pipermail/beginners/attachments/20170926/27335551/attachment.html>
------------------------------
Subject: Digest Footer
_______________________________________________
Beginners mailing list
[email protected]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
------------------------------
End of Beginners Digest, Vol 111, Issue 18
******************************************