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. Re: the first argument of take is an Int and not Integral..
(Sunil S Nandihalli)
2. Re: Why ShowS? (Christopher Howard)
3. Re: Why ShowS? (Brandon Allbery)
4. Re: Why ShowS? (Yitzchak Gale)
----------------------------------------------------------------------
Message: 1
Date: Thu, 11 Aug 2011 00:28:30 +0530
From: Sunil S Nandihalli <[email protected]>
Subject: Re: [Haskell-beginners] the first argument of take is an Int
and not Integral..
To: Daniel Fischer <[email protected]>
Cc: [email protected]
Message-ID:
<cap0fd71tmp4cvqvzsjajgfqc82q7jsxwjvwpewsuc6n7x3e...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
ah that is exactly what I wanted .. thanks Daniel.
Sunil.
On Thu, Aug 11, 2011 at 12:22 AM, Daniel Fischer <
[email protected]> wrote:
> On Wednesday 10 August 2011, 20:42:05, Sunil S Nandihalli wrote:
> > hmm.. I do agree that taking elements larger than say 2-billion is
> > insane .. however, I have no intention of doing that. But the thing is
> > the value I am getting is of type Integer but it actually very small
> > and I am unable to pass it as is. So is there a way to convert a value
> > of type Integer to Int ? This might be obvious.. But I am a newbie to
> > the whole Haskell thing ...
>
> Use fromInteger or fromIntegral
>
> >
> > Thanks,
> > Sunil.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20110811/3890c7ff/attachment-0001.htm>
------------------------------
Message: 2
Date: Wed, 10 Aug 2011 18:34:20 -0800
From: Christopher Howard <[email protected]>
Subject: Re: [Haskell-beginners] Why ShowS?
To: Haskell Beginners <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8; format=flowed
On 08/09/2011 09:10 PM, Brandon Allbery wrote:
> On Wed, Aug 10, 2011 at 00:41, yi huang <[email protected]
> <mailto:[email protected]>> wrote:
>
> (1) string concatenation: step through the list of Char to
> reach the end, then append the second string; this is linear in
> the length of "the quick brown fox...".
>
> (2) list concatenation: start with [], append "the quick brown
> fox...", append "now is the time...". Each concatenation is
> linear in the number of strings already concatenated, so becomes
> expensive as more stings are appended.
>
> (3) function composition: (in effect) start with (const ""),
> compose it with (++ "the quick brown fox...:), compose it with
> (++ "now is the time..."). Each concatenation is constant time,
> since it's simply applying (.) to what it has.
>
>
> I don't understand, since (++) is lazy, the operation itself is very
> cheap, only traversing the concatenated list need O(n) time
> complexity, isn't that right?
>
>
> Yes, but that's precisely what's being avoided. The assumption for
> ShowS is that you'll have to traverse it anyway to output it; ShowS
> avoids doing so multiple times.
>
> Now, as to how useful it is... you'll note there isn't actually a lot of
> code out there that uses ShowS (or functional concatenation in general).
> The simplistic show/read system uses it, but most other parsers and
> string generators use other mechanisms instead: Monad, Applicative,
> occasionally Arrow. (ShowS/ReadS predates all three, I believe. There
> may have also been additional advantages with pre-monadic/Gofer-style I/O.)
>
> There are also questions of stack/heap complexity; while it may save
> some time to build up a large output string this way, it also builds up
> quite a few thunks, whereas (especially with fusion) a more direct
> concatenation system may have linear heap complexity and constant stack
> complexity.
>
> (Also, coming at it from a systems perspective, I can't help but think
> that I/O time dominates concatenation time in the vast majority of cases.)
>
> --
> brandon s allbery [email protected] <mailto:[email protected]>
> wandering unix systems administrator (available) (412) 475-9364 vm/sms
>
One of the reasons I asked: I was wondering if Haskell already had a
library that would take an Int/Integer and convert it to the string
representations of other bases; or if this was a chore that still needed
to be taken care of by someone. I found the Numeric module, but then see
that all relevant functions return this weird "ShowS" instead of a
String. So... is that how everyone likes it? Or is there another module
people use for base conversion...?
--
frigidcode.com
theologia.indicium.us
------------------------------
Message: 3
Date: Thu, 11 Aug 2011 01:16:25 -0400
From: Brandon Allbery <[email protected]>
Subject: Re: [Haskell-beginners] Why ShowS?
To: Christopher Howard <[email protected]>
Cc: Haskell Beginners <[email protected]>
Message-ID:
<cakfcl4wrqzrtidjdxh_8c4bz7iwrxvabwjz_qk2phsofcch...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
On Wed, Aug 10, 2011 at 22:34, Christopher Howard <
[email protected]> wrote:
> One of the reasons I asked: I was wondering if Haskell already had a
> library that would take an Int/Integer and convert it to the string
> representations of other bases; or if this was a chore that still needed to
> be taken care of by someone. I found the Numeric module, but then see that
> all relevant functions return this weird "ShowS" instead of a String. So...
> is that how everyone likes it? Or is there another module people use for
> base conversion...?
I think it isn't used often enough for anyone to bother reworking it,
especially since you can sidestep the ShowS stuff by invoking e.g. (showHex
myNum "") which nets you an ordinary String. (Parse this as ((showHex
myNum) ""), where (showHex myNum) produces a ShowS (function from String to
String) which is then applied to ("").) You could also cheat if you have
something to append to the result of (showHex) by passing that instead of
("").
--
brandon s allbery [email protected]
wandering unix systems administrator (available) (412) 475-9364 vm/sms
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20110811/cfc43a8b/attachment-0001.htm>
------------------------------
Message: 4
Date: Thu, 11 Aug 2011 12:13:59 +0300
From: Yitzchak Gale <[email protected]>
Subject: Re: [Haskell-beginners] Why ShowS?
To: Brandon Allbery <[email protected]>
Cc: Haskell Beginners <[email protected]>
Message-ID:
<caorualboz9y49aa216zzo+--m7bme62-ywxo1wau7sj++5m...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1
Brandon Allbery wrote:
> Now, as to how useful it is... you'll note there isn't actually a lot of
> code out there that uses ShowS
I use it sometimes.
ShowS was the first and simplest, "pretty-printer" library.
A pretty-printer library is an alternative to big ugly monolithic
expressions for creating output.
Because it is based on function composition, ShowS makes
it easier to write in a nice combinator style. All of the early
pretty-printer libraries used function composition in this way.
It appears early library writers assumed that because of its
advantages, and also because of certain limitations of
early compilers, everyone would always use ShowS
instead of directly constructing Strings. So they used
ShowS for the types of showHex and showOct. In reality
ShowS never caught on, but it's not worth the trouble
to change those original types now.
>?The simplistic show/read system uses it, but most other
> parsers and string generators use other mechanisms instead:
> Monad, Applicative, occasionally Arrow.
Recently the trend is to use "builders" based on Monoid
for fast rendering libraries. That style has some of the
advantages of ShowS, plus, unlike ShowS, it works
equally well with ByteString and Text.
While not the fastest or most featureful, ShowS still has the
advantages of being really simple to use, available directly
from the Prelude without any external library (and thus
a standard style, even though it isn't as popular as it
could be), and it generally has a pleasing simplifying
effect on the surrounding code. It also works absolutely
seamlessly with showHex and showOct :).
Any big expression of type String that starts with
"concat" followed by a long list of String expressions
is a good candidate for using ShowS instead.
Here is a simple example of how to use ShowS:
showPerson :: String -> Int -> Int -> String
showPerson name age shoeSize =
concat [ "Name: ", name, ", Age: ", show age,
", Shoe size: ", show shoeSize]
...
putStrLn $ showPerson name age shoe
becomes, in the ShowS style:
showsPerson :: String -> Int -> Int -> ShowS
showsPerson name age shoeSize =
("Name: " ++) . (name ++) . (", Age: " ++) . shows age .
(", Shoe size: " ++) . shows shoeSize
...
putStrLn $ showsPerson name age shoe ""
Regards,
Yitz
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 38, Issue 25
*****************************************