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:  Why ShowS? (yi huang)
   2.  New style exceptions... (Manfred Lotz)
   3. Re:  Why ShowS? (Brandon Allbery)
   4. Re:  New style exceptions... (Manfred Lotz)


----------------------------------------------------------------------

Message: 1
Date: Wed, 10 Aug 2011 12:41:06 +0800
From: yi huang <[email protected]>
Subject: Re: [Haskell-beginners] Why ShowS?
To: Brandon Allbery <[email protected]>
Cc: Haskell Beginners <[email protected]>
Message-ID:
        <cahu7ryz4v-968yheh7zrvyo9p95pyu-pstufq1buhjbw9yh...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

On Wed, Aug 10, 2011 at 12:03 PM, Brandon Allbery <[email protected]>wrote:

> On Tue, Aug 9, 2011 at 23:34, Christopher Howard <
> [email protected]> wrote:
>
>> What is meant here by "constant-time concatenation of results using
>> function composition"? Perhaps someone could provide an example?
>
>
> In effect, ShowS concatenates chunks instead of strings; you could think of
> it as building a list of strings, except that even then concatenation means
> going to the end of the list to append the new item, whereas the
> right-biased precedence of function composition means that it's already
> holding a pointer to the end of the "list" (instead of the beginning,
> as with normal lists).
>
> So, concatenating
> > "the quick brown fox jumps over the lazy dog"
> and
> > "now is the time for all good endofunctors to come to the aid of their
> category"
>
> your choices are:
>
> (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?


>
> In all three cases, you have essentially the same time complexity in
> outputting the result.
>
> --
> brandon s allbery                                      [email protected]
> wandering unix systems administrator (available)     (412) 475-9364 vm/sms
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>


-- 
http://www.yi-programmer.com/blog/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20110810/ae7cf5d5/attachment-0001.htm>

------------------------------

Message: 2
Date: Wed, 10 Aug 2011 07:01:42 +0200
From: Manfred Lotz <[email protected]>
Subject: [Haskell-beginners] New style exceptions...
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=US-ASCII

Hi there,
Exception handling in RWH is called old-style and deprecated. I didn't
find a tutorial telling how to do the new style.

Here I created two examples both dealing with an exception when
creating a directory.

I would like to know if the way I did is ok or not.
Improvements resp. corrections are most welcome

First way:

<--------------snip------------------------------------>
{-# LANGUAGE ScopedTypeVariables #-}
module Main where

import System.Exit
import System.Directory
import Control.Exception

import Prelude hiding ( catch, readFile )

main :: IO ()
main = do 
  handle (\(e :: SomeException) -> print $ show e) $ do
    createDirectoryIfMissing True "/tmr/a/b/c/d"
    _ <- exitWith $ ExitFailure 1
    return ()
  print "Directory created successfully"
<--------------snap------------------------------------>


Second way:


<--------------snip------------------------------------>
module Main where

import System.Exit
import System.Directory
import Control.Exception

import Prelude hiding ( catch, readFile )

main :: IO ()
main = do 
  catch (createDirectoryIfMissing True "/tmr/a/b/c/d") exhandler
  print "Directory created successfully"
  where
    exhandler :: SomeException -> IO ()
    exhandler e = do print e
                     _ <- exitWith $ ExitFailure 1
                     return ()
<--------------snap------------------------------------>


 

-- 
Tnanks, 
Manfred





------------------------------

Message: 3
Date: Wed, 10 Aug 2011 01:10:03 -0400
From: Brandon Allbery <[email protected]>
Subject: Re: [Haskell-beginners] Why ShowS?
To: yi huang <[email protected]>
Cc: Haskell Beginners <[email protected]>
Message-ID:
        <CAKFCL4Xg6xDRweYZ7yqyzwTY55c2aFy=-mc2TAhufgd=n1+...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

On Wed, Aug 10, 2011 at 00:41, yi huang <[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]
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/20110810/096a3524/attachment-0001.htm>

------------------------------

Message: 4
Date: Wed, 10 Aug 2011 08:34:58 +0200
From: Manfred Lotz <[email protected]>
Subject: Re: [Haskell-beginners] New style exceptions...
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=US-ASCII

On Wed, 10 Aug 2011 07:01:42 +0200
Manfred Lotz <[email protected]> wrote:

> Hi there,
> Exception handling in RWH is called old-style and deprecated. I didn't
> find a tutorial telling how to do the new style.
> 
> Here I created two examples both dealing with an exception when
> creating a directory.
> 
> I would like to know if the way I did is ok or not.
> Improvements resp. corrections are most welcome
> 
> First way:
> 
> <--------------snip------------------------------------>
> {-# LANGUAGE ScopedTypeVariables #-}
> module Main where
> 
> import System.Exit
> import System.Directory
> import Control.Exception
> 
> import Prelude hiding ( catch, readFile )
> 
> main :: IO ()
> main = do 
>   handle (\(e :: SomeException) -> print $ show e) $ do
>     createDirectoryIfMissing True "/tmr/a/b/c/d"
>     _ <- exitWith $ ExitFailure 1
>     return ()
>   print "Directory created successfully"
> <--------------snap------------------------------------>
> 
> 

This was stupid. Should be like this:



main :: IO ()
main = do 
  handle (\(e :: SomeException) -> do print $ show e
                                      _ <- exitWith $ ExitFailure 1
                                      return ()
         ) $ do
    createDirectoryIfMissing True "/tmr/a/b/c/d"
  print "Directory created successfully"



-- 
Manfred





------------------------------

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 38, Issue 22
*****************************************

Reply via email to