Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://www.haskell.org/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. Chain computations (Tim Baumgartner)
2. Re: Chain computations (Brent Yorgey)
3. Re: Chain computations (Tim Baumgartner)
4. Re: Chain computations (Brent Yorgey)
5. Parsing an integer (Tim Baumgartner)
6. Re: Parsing an integer (Patrick LeBoutillier)
7. Re: Parsing an integer (Stephen Tetley)
8. Re: Parsing an integer (Tim Baumgartner)
9. Re: Ignoring the result of a monadic computation
(Magnus Therning)
----------------------------------------------------------------------
Message: 1
Date: Sat, 20 Nov 2010 18:10:22 +0100
From: Tim Baumgartner <[email protected]>
Subject: [Haskell-beginners] Chain computations
To: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Hi,
I really like fooling around with operators. In elementary school, we
always had to make chain computations, e.g.
teacher: 2
teacher: +1
teacher: *2
teacher: divided by 3
Again, I didn't find the following operator on hoogle (very similar to ($)):
(->>) :: a -> (a -> b) -> b
a ->> f = f a
Using it, we can nicely write down the above computation:
2 ->> (+1) ->> (*2) ->> (`div` 3)
Cheers
Tim
------------------------------
Message: 2
Date: Sat, 20 Nov 2010 12:33:41 -0500
From: Brent Yorgey <[email protected]>
Subject: Re: [Haskell-beginners] Chain computations
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
On Sat, Nov 20, 2010 at 06:10:22PM +0100, Tim Baumgartner wrote:
> Hi,
>
> I really like fooling around with operators. In elementary school, we
> always had to make chain computations, e.g.
>
> teacher: 2
> teacher: +1
> teacher: *2
> teacher: divided by 3
>
> Again, I didn't find the following operator on hoogle (very similar to ($)):
> (->>) :: a -> (a -> b) -> b
> a ->> f = f a
You can define (->>) as flip ($).
>
> Using it, we can nicely write down the above computation:
> 2 ->> (+1) ->> (*2) ->> (`div` 3)
Another way to write this is using (>>>) from Control.Category which
is essentially flipped function composition. Occasionally I will
write things in this style and define (>$>) to be the same as your
(->>), and write things like
2 >$> (+1) >>> (*2) >>> (`div` 3)
If we did the same thing in right-to-left style, your code would
correspond to
(`div` 3) $ (*2) $ (+1) $ 2
whereas mine corresponds to
(`div` 3) . (*2) . (+1) $ 2
The latter is generally considered better style, since any subsection
of the "pipeline" on the left side of the $ is valid on its own,
making it much easier to refactor (pull out pieces of the pipeline
that are also used elsewhere and give them a name, and so on). Also,
this form makes it clear that the 2 at the end is different. Simply
removing the $ 2 gives a valid function that abstracts away from the
specific starting value and will work for any starting value.
-Brent
------------------------------
Message: 3
Date: Sat, 20 Nov 2010 19:32:16 +0100
From: Tim Baumgartner <[email protected]>
Subject: Re: [Haskell-beginners] Chain computations
To: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Thanks Brent,
I think I understood your point: In my form a ->> f ->> g, there are
parentheses like (a ->> f) ->> g, and the parenth expression resolves
to a value, whereas in a >$> f >>> g, there are parentheses like a >$>
(f >>> g), and the parenth expression is a function which is much more
flexible.
Tim
2010/11/20 Brent Yorgey <[email protected]>:
> On Sat, Nov 20, 2010 at 06:10:22PM +0100, Tim Baumgartner wrote:
>> Hi,
>>
>> I really like fooling around with operators. In elementary school, we
>> always had to make chain computations, e.g.
>>
>> teacher: 2
>> teacher: +1
>> teacher: *2
>> teacher: divided by 3
>>
>> Again, I didn't find the following operator on hoogle (very similar to ($)):
>> (->>) :: a -> (a -> b) -> b
>> a ->> f = f a
>
> You can define (->>) as flip ($).
>
>>
>> Using it, we can nicely write down the above computation:
>> 2 ->> (+1) ->> (*2) ->> (`div` 3)
>
> Another way to write this is using (>>>) from Control.Category which
> is essentially flipped function composition. Occasionally I will
> write things in this style and define (>$>) to be the same as your
> (->>), and write things like
>
> 2 >$> (+1) >>> (*2) >>> (`div` 3)
>
> If we did the same thing in right-to-left style, your code would
> correspond to
>
> (`div` 3) $ (*2) $ (+1) $ 2
>
> whereas mine corresponds to
>
> (`div` 3) . (*2) . (+1) $ 2
>
> The latter is generally considered better style, since any subsection
> of the "pipeline" on the left side of the $ is valid on its own,
> making it much easier to refactor (pull out pieces of the pipeline
> that are also used elsewhere and give them a name, and so on). Also,
> this form makes it clear that the 2 at the end is different. Simply
> removing the $ 2 gives a valid function that abstracts away from the
> specific starting value and will work for any starting value.
>
> -Brent
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
------------------------------
Message: 4
Date: Sat, 20 Nov 2010 18:12:18 -0500
From: Brent Yorgey <[email protected]>
Subject: Re: [Haskell-beginners] Chain computations
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
On Sat, Nov 20, 2010 at 07:32:16PM +0100, Tim Baumgartner wrote:
> Thanks Brent,
>
> I think I understood your point: In my form a ->> f ->> g, there are
> parentheses like (a ->> f) ->> g, and the parenth expression resolves
> to a value, whereas in a >$> f >>> g, there are parentheses like a >$>
> (f >>> g), and the parenth expression is a function which is much more
> flexible.
Yes, that's a very good way of putting it.
-Brent
------------------------------
Message: 5
Date: Sun, 21 Nov 2010 13:27:06 +0100
From: Tim Baumgartner <[email protected]>
Subject: [Haskell-beginners] Parsing an integer
To: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=UTF-8
Hi folks,
since I always get great answers here, I have another question. I'm
currently parsing a (positive) integer using
data Token = Number Int -- ...
number :: Parser Token
number = many1 digit â (read >>> Number >>> return)
-- = many1 digit â returnâNumberâread
But I wonder what an advanced haskeller would code instead.
Particularly, I'd like to get rid of the 'return'.
Thanks in advance
Tim
------------------------------
Message: 6
Date: Sun, 21 Nov 2010 09:09:04 -0500
From: Patrick LeBoutillier <[email protected]>
Subject: Re: [Haskell-beginners] Parsing an integer
To: Tim Baumgartner <[email protected]>
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=UTF-8
Tim,
On Sun, Nov 21, 2010 at 7:27 AM, Tim Baumgartner
<[email protected]> wrote:
>
> data Token = Number Int  -- ...
> number :: Parser Token
> number = many1 digit â (read >>> Number >>> return)
> -- = many1 digit â returnâNumberâread
>
> But I wonder what an advanced haskeller would code instead.
> Particularly, I'd like to get rid of the 'return'.
My understanding is that to remove the return, you can lift your pure
function (Number . read) into the Parser monad using fmap or <$> from
Control.Applicative:
number' = Number . read <$> many1 digit
A bit of Sunday morning exploration yielded that you may also like the
<$$> operator, but it is not defined in the standard libraries (see
discussion here:
http://www.haskell.org/pipermail/libraries/2010-April/013403.html) :
(<$$>) :: Functor f => f a -> (a -> b) -> f b
(<$$>) = flip (<$>)
infixl 4 <$$>
number' = many1 digit <$$> Number . read
or
number' = many1 digit <$$> (read >>> Number)
Patrick
>
> Thanks in advance
> Tim
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
--
=====================
Patrick LeBoutillier
Rosemère, Québec, Canada
------------------------------
Message: 7
Date: Sun, 21 Nov 2010 14:50:56 +0000
From: Stephen Tetley <[email protected]>
Subject: Re: [Haskell-beginners] Parsing an integer
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Where is the Parser monad coming from?
If its Parsec, a "seasoned Haskeller" would avoid read (which is
re-parsing already parsed data) and use the integer parser from
Parsec's Token parser module.[*]
[*] The one caveat is that the number parsers in this module follow
Haskell's lexical rules - you might not always want this.
------------------------------
Message: 8
Date: Sun, 21 Nov 2010 16:19:48 +0100
From: Tim Baumgartner <[email protected]>
Subject: Re: [Haskell-beginners] Parsing an integer
To: Stephen Tetley <[email protected]>
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Hi Stephen,
yes, it's Parsec, but I write this parser only for learning the
language, so I don't care if I rewrite existing code. But thanks for
the advice regarding performance. Patrick's answer was perfect for me
and I will get used to
<$> = fmap
soon.
Tim
2010/11/21 Stephen Tetley <[email protected]>:
> Where is the Parser monad coming from?
>
> If its Parsec, a "seasoned Haskeller" would avoid read (which is
> re-parsing already parsed data) and use the integer parser from
> Parsec's Token parser module.[*]
>
> [*] The one caveat is that the number parsers in this module follow
> Haskell's lexical rules - you might not always want this.
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
------------------------------
Message: 9
Date: Sun, 21 Nov 2010 16:10:50 +0000
From: Magnus Therning <[email protected]>
Subject: Re: [Haskell-beginners] Ignoring the result of a monadic
computation
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"
On 19/11/10 19:20, Brent Yorgey wrote:
> On Fri, Nov 19, 2010 at 03:37:10PM +0000, Magnus Therning wrote:
>> On Fri, Nov 19, 2010 at 15:31, Brent Yorgey <[email protected]> wrote:
>>> On Fri, Nov 19, 2010 at 03:26:04PM +0000, Magnus Therning wrote:
>>>> On Fri, Nov 19, 2010 at 15:21, Brent Yorgey <[email protected]> wrote:
>>>>> On Fri, Nov 19, 2010 at 07:56:02AM +0100, Tim Baumgartner wrote:
>>>>>> Hi,
>>>>>>
>>>>>> while learning about monads, I had something like
>>>>>>
>>>>>> do
>>>>>> line <- getLine
>>>>>> something
>>>>>> putStrLn line
>>>>>>
>>>>>> and I wondered if I could write it in one line, without naming of
>>>>>> parameters.
>>>>>> I finally came up with
>>>>>>
>>>>>> getLine >>= ignore something >>= putStrLn
>>>>>>
>>>>>> using
>>>>>> ignore :: Monad m => m a -> b -> m b
>>>>>> ignore m a = m >> return a
>>>>>>
>>>>>> I'm satisfied with this solution but searching hoogle I didn't find
>>>>>> a standard function for my ignore. Am I missing something?
>>>>>
>>>>> Nope, there isn't such a function, but I like it. It reminds me of
>>>>> (*>) and (<*) from Control.Applicative. Note that you sometimes see
>>>>> the name 'ignore' used for a slightly different function, namely
>>>>>
>>>>> ignore :: Monad m => m a -> m ()
>>>>> ignore m = m >> return ()
>>>>>
>>>>> but yours is a bit more general. Other names for your function might
>>>>> be 'passThrough' or something like that.
>>>>
>>>> I'm not sure I see any benefit of ': m a -> b -> m b' over 'm a -> m
>>>> ()'. When would you want to use the former?
>>>
>>> >From the OP's message:
>>>
>>> getLine >>= ignore something >>= putStrLn
>>>
>>> which executes 'something' for its side effect and passes the result
>>> of getLine through to putStrLn, without ever having to give a name to
>>> the result of getLine. IIUC this was the whole point.
>>
>> IMNSHO, not such a strong argument for such a function though. :-)
>
> It depends what the "argument" is about. I agree there is not a
> strong argument for including such a function in the standard
> libraries. But it's a perfectly nice function to define and use in
> one's own code. Haskell makes this sort of abstraction very cheap.
I can agree with that.
/M
--
Magnus Therning (OpenPGP: 0xAB4DFBA4)
magnusï¼ therningï¼org Jabber: magnusï¼ therningï¼org
http://therning.org/magnus identi.ca|twitter: magthe
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 262 bytes
Desc: OpenPGP digital signature
Url :
http://www.haskell.org/pipermail/beginners/attachments/20101121/8036ef8b/signature.bin
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 29, Issue 30
*****************************************