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.  Newbie performance question (Jordan Ellis)
   2. Re:  Newbie performance question (Ozgur Akgun)
   3. Re:  Newbie performance question (Daniel Fischer)
   4.  A problem with a simple csv file parser (Andy Elvey)
   5. RE:  Newbie performance question (Jordan Ellis)
   6. Re:  A problem with a simple csv file parser (Jimmy Wylie)
   7. Re:  A problem with a simple csv file parser (Andy Elvey)


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

Message: 1
Date: Fri, 15 Oct 2010 15:03:42 -0700
From: Jordan Ellis <[email protected]>
Subject: [Haskell-beginners] Newbie performance question
To: <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"


I'm trying (really trying!) to get my head around Haskell, but one (among many) 
significant stumbling blocks for me so far has been trying to figure out how my 
code is actually going to perform. Here's a contrived example of the sort of 
thing that confuses me. Let's say I have a function which produces a list of 
odd integers from 1 to n:
f n = filter (odd) [1..n]
...and I have another function that makes a list of all sums of pairs of odd 
numbers from 1 to n:
g n = [a + b | a <- (f n), b <- (f n)]
I assume that in most languages, (f n) would get called twice (once for a, and 
once for b), but what happens in Haskell? Does GHC recognize that, since f is a 
pure function, it only has to be evaluated once for a given n? If not, would a 
'where clause' make any difference? e.g.,
g n = [a + b | a <- h, b <- h] where h = f n
Thanks.                                           
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20101015/81e7d06b/attachment-0001.html

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

Message: 2
Date: Fri, 15 Oct 2010 23:26:15 +0100
From: Ozgur Akgun <[email protected]>
Subject: Re: [Haskell-beginners] Newbie performance question
To: Jordan Ellis <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="utf-8"

Just a quick reply, others might go into more detail.

On 15 October 2010 23:03, Jordan Ellis <[email protected]> wrote:

> Let's say I have a function which produces a list of odd integers from 1 to
> n:
>
> f n = filter (odd) [1..n]
>
> ...and I have another function that makes a list of all sums of pairs of
> odd numbers from 1 to n:
>
> g n = [a + b | a <- (f n), b <- (f n)]
>
> I assume that in most languages, (f n) would get called twice (once for a,
> and once for b), but what happens in Haskell? Does GHC recognize that, since
> f is a pure function, it only has to be evaluated once for a given n?
>

No, it doesn't. Haskell compilers doesn't do
CSE<http://en.wikipedia.org/wiki/Common_subexpression_elimination>
.


> If not, would a 'where clause' make any difference? e.g.,
>
> g n = [a + b | a <- h, b <- h] where h = f n
>

Yes, this is the way to go (or local let bindings).

HTH,
Ozgur
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20101015/ad2bc41b/attachment-0001.html

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

Message: 3
Date: Sat, 16 Oct 2010 00:57:41 +0200
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Newbie performance question
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain;  charset="utf-8"

On Saturday 16 October 2010 00:03:42, Jordan Ellis wrote:
> I'm trying (really trying!) to get my head around Haskell, but one
> (among many) significant stumbling blocks for me so far has been trying
> to figure out how my code is actually going to perform. Here's a
> contrived example of the sort of thing that confuses me. Let's say I
> have a function which produces a list of odd integers from 1 to n: f n =
> filter (odd) [1..n]
> ...and I have another function that makes a list of all sums of pairs of
> odd numbers from 1 to n: g n = [a + b | a <- (f n), b <- (f n)]
> I assume that in most languages, (f n) would get called twice (once for
> a, and once for b), but what happens in Haskell? Does GHC recognize
> that, since f is a pure function, it only has to be evaluated once for a
> given n?

That depends. If you compile without optimisations, the list f n won't be 
shared (that may of course change in future versions), if you compile with 
optimisations, it will be shared.

GHC does very little common subexpression elimination, and CSE is not 
always a good thing.

In the case above, it's fine for small n, but consider what happens for 
large n. If the list is not shared, for every a you get a new application 
of f (at least, that's GHC's current behaviour), and the algorithm runs in 
constant space. If the list is shared, the entire list remains in memory 
after it has been constructed for the first a. Thus the algorithm runs in 
O(n) space (but it runs faster unless n is really large).
In such cases, you can usually suppress undesired sharing with -fno-cse.

> If not, would a 'where clause' make any difference? e.g., g n =
> [a + b | a <- h, b <- h] where h = f n

Yes, when compiled without optimisations, no when compiled with.
In general, when you want a value to be shared, give it a name.
It may be shared if you don't but with a name, a decent implementation will 
share it.

> Thanks.



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

Message: 4
Date: Sat, 16 Oct 2010 18:23:04 +1300
From: Andy Elvey <[email protected]>
Subject: [Haskell-beginners] A problem with a simple csv file parser
To: beginners <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

Hi all -

 I'm just trying to get the simple csv file parser from "Real World 
Haskell" working.
However, I've got a problem. Here's the code  -

--  A simple csv parser 
Module parseCSV where
import Text.ParserCombinators.Parsec

csvFile = endBy line eol
line = sepBy cell (char ',')
cell = many (noneOf ",\n")
eol = char '\n'

parseCSV :: String -> Either ParseError [[String]]
parseCSV input = parse csvFile "(unknown)" input

( Btw, I'm running Ubuntu 10.04, GHC 6.12.1  ) 

When I try to run this (using "runhaskell parseCSV.hs test.csv" ) I get 
the error -
parseCSV.hs:4:16: parse error on input `where'

This has me really puzzled - as far as I can see, the first line looks 
identical to a number of similar
programs that I've found (while searching for a solution to this).  So, 
if anyone can let me know
what is causing this, that'd be great!  Many thanks in advance -
- Andy





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

Message: 5
Date: Fri, 15 Oct 2010 23:18:39 -0700
From: Jordan Ellis <[email protected]>
Subject: RE: [Haskell-beginners] Newbie performance question
To: <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"


Thanks for the reply. I have a quick follow-up question: With optimization 
enabled, will a value be shared between recursive function calls? For example, 
in the following:
myRecursiveFunction 0 m = []myRecursiveFunction n m = m : myRecursiveFunction 
(n - 1) m
...will 'm' be calculated 'n' times, or just once? Thanks.
> From: [email protected]
> To: [email protected]
> Subject: Re: [Haskell-beginners] Newbie performance question
> Date: Sat, 16 Oct 2010 00:57:41 +0200
> CC: [email protected]
> 
> On Saturday 16 October 2010 00:03:42, Jordan Ellis wrote:
> > I'm trying (really trying!) to get my head around Haskell, but one
> > (among many) significant stumbling blocks for me so far has been trying
> > to figure out how my code is actually going to perform. Here's a
> > contrived example of the sort of thing that confuses me. Let's say I
> > have a function which produces a list of odd integers from 1 to n: f n =
> > filter (odd) [1..n]
> > ...and I have another function that makes a list of all sums of pairs of
> > odd numbers from 1 to n: g n = [a + b | a <- (f n), b <- (f n)]
> > I assume that in most languages, (f n) would get called twice (once for
> > a, and once for b), but what happens in Haskell? Does GHC recognize
> > that, since f is a pure function, it only has to be evaluated once for a
> > given n?
> 
> That depends. If you compile without optimisations, the list f n won't be 
> shared (that may of course change in future versions), if you compile with 
> optimisations, it will be shared.
> 
> GHC does very little common subexpression elimination, and CSE is not 
> always a good thing.
> 
> In the case above, it's fine for small n, but consider what happens for 
> large n. If the list is not shared, for every a you get a new application 
> of f (at least, that's GHC's current behaviour), and the algorithm runs in 
> constant space. If the list is shared, the entire list remains in memory 
> after it has been constructed for the first a. Thus the algorithm runs in 
> O(n) space (but it runs faster unless n is really large).
> In such cases, you can usually suppress undesired sharing with -fno-cse.
> 
> > If not, would a 'where clause' make any difference? e.g., g n =
> > [a + b | a <- h, b <- h] where h = f n
> 
> Yes, when compiled without optimisations, no when compiled with.
> In general, when you want a value to be shared, give it a name.
> It may be shared if you don't but with a name, a decent implementation will 
> share it.
> 
> > Thanks.
> 
                                          
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20101016/888e3786/attachment-0001.html

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

Message: 6
Date: Sat, 16 Oct 2010 01:48:15 -0500
From: Jimmy Wylie <[email protected]>
Subject: Re: [Haskell-beginners] A problem with a simple csv file
        parser
To: <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset="ISO-8859-1"; format=flowed

  Hi Andy,

If I remember right, runhaskell is expecting a full fledged haskell 
program with a "main" defined. Instead, you've sent it a module, which 
it isn't expecting, so you're getting a parse error on the "where".  If 
you remove the "Module parseCSV where" from the top of your file, and 
try again, you'll get a "not in scope 'main' " error.

So you can test this a couple of ways.  An easy way would be to open a 
terminal window and type "ghci", to open up ghc's Interpreter. Then, at 
the prompt type:

:l parseCSV.hs (or whatever the name of your file is)

Now, ghci, has loaded up all the functions defined in your file. So now, 
you can invoke your functions:

*Main> parseCSV "a,b,c,d\n"
Right [["a","b","c","d"]]

If you're really adamant about using runhaskell, what you need to do is 
define a main function that opens up a file, use hGetContents to read 
the file as a String, and then invoke parseCSV on that String.

In order to take command line arguments, you'll want this:
http://haskell.org/ghc/docs/6.12.1/html/libraries/base-4.2.0.0/System-Environment.html#v:getArgs


Good Luck,
Jimmy

On 10/16/10 12:23 AM, Andy Elvey wrote:
> Hi all -
>
>   I'm just trying to get the simple csv file parser from "Real World
> Haskell" working.
> However, I've got a problem. Here's the code  -
>
> --  A simple csv parser
> Module parseCSV where
> import Text.ParserCombinators.Parsec
>
> csvFile = endBy line eol
> line = sepBy cell (char ',')
> cell = many (noneOf ",\n")
> eol = char '\n'
>
> parseCSV :: String ->  Either ParseError [[String]]
> parseCSV input = parse csvFile "(unknown)" input
>
> ( Btw, I'm running Ubuntu 10.04, GHC 6.12.1  )
>
> When I try to run this (using "runhaskell parseCSV.hs test.csv" ) I get
> the error -
> parseCSV.hs:4:16: parse error on input `where'
>
> This has me really puzzled - as far as I can see, the first line looks
> identical to a number of similar
> programs that I've found (while searching for a solution to this).  So,
> if anyone can let me know
> what is causing this, that'd be great!  Many thanks in advance -
> - Andy
>
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>




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

Message: 7
Date: Sat, 16 Oct 2010 20:09:45 +1300
From: Andy Elvey <[email protected]>
Subject: Re: [Haskell-beginners] A problem with a simple csv file
        parser
To: beginners <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

Hi Jimmy -

Thanks very much for that!  I'll try that - it sounds good to me!  
Bye for now -
- Andy 


Jimmy Wylie wrote:
>  Hi Andy,
>
> If I remember right, runhaskell is expecting a full fledged haskell 
> program with a "main" defined. Instead, you've sent it a module, which 
> it isn't expecting, so you're getting a parse error on the "where".  
> If you remove the "Module parseCSV where" from the top of your file, 
> and try again, you'll get a "not in scope 'main' " error.
>
> So you can test this a couple of ways.  An easy way would be to open a 
> terminal window and type "ghci", to open up ghc's Interpreter. Then, 
> at the prompt type:
>
> :l parseCSV.hs (or whatever the name of your file is)
>
> Now, ghci, has loaded up all the functions defined in your file. So 
> now, you can invoke your functions:
>
> *Main> parseCSV "a,b,c,d\n"
> Right [["a","b","c","d"]]
>
> If you're really adamant about using runhaskell, what you need to do 
> is define a main function that opens up a file, use hGetContents to 
> read the file as a String, and then invoke parseCSV on that String.
>
> In order to take command line arguments, you'll want this:
> http://haskell.org/ghc/docs/6.12.1/html/libraries/base-4.2.0.0/System-Environment.html#v:getArgs
>  
>
>
>
> Good Luck,
> Jimmy
>
> On 10/16/10 12:23 AM, Andy Elvey wrote:
>> Hi all -
>>
>>   I'm just trying to get the simple csv file parser from "Real World
>> Haskell" working.
>> However, I've got a problem. Here's the code  -
>>
>> --  A simple csv parser
>> Module parseCSV where
>> import Text.ParserCombinators.Parsec
>>
>> csvFile = endBy line eol
>> line = sepBy cell (char ',')
>> cell = many (noneOf ",\n")
>> eol = char '\n'
>>
>> parseCSV :: String ->  Either ParseError [[String]]
>> parseCSV input = parse csvFile "(unknown)" input
>>
>> ( Btw, I'm running Ubuntu 10.04, GHC 6.12.1  )
>>
>> When I try to run this (using "runhaskell parseCSV.hs test.csv" ) I get
>> the error -
>> parseCSV.hs:4:16: parse error on input `where'
>>
>> This has me really puzzled - as far as I can see, the first line looks
>> identical to a number of similar
>> programs that I've found (while searching for a solution to this).  So,
>> if anyone can let me know
>> what is causing this, that'd be great!  Many thanks in advance -
>> - Andy
>>
>>
>>
>> _______________________________________________
>> Beginners mailing list
>> [email protected]
>> http://www.haskell.org/mailman/listinfo/beginners
>>
>>
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>



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

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


End of Beginners Digest, Vol 28, Issue 25
*****************************************

Reply via email to