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.  [Int] oddness with summing large lists (Philip Scott)
   2. Re:  [Int] oddness with summing large lists (Philip Scott)
   3. Re:  [Int] oddness with summing large lists (Daniel Fischer)
   4.  Feedback on google code jam practice (Aur Saraf)
   5. Re:  Re: Iterating through a list of char... (Hein Hundal)
   6. Re:  Re: Iterating through a list of char... (jean verdier)
   7. Re:  Re: Iterating through a list of char... (David Virebayre)
   8. Re:  Re: Iterating through a list of char... (matthew coolbeth)


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

Message: 1
Date: Wed, 28 Apr 2010 22:32:10 +0100
From: Philip Scott <[email protected]>
Subject: [Haskell-beginners] [Int] oddness with summing large lists
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

Hi all,

I kind of stumbled into this while doing something else - but I thought 
it was worth posting since I have never actually managed to crash ghci 
before :)

Prelude> let d = [1..1000000000000] :: [Int]
Prelude> sum d
0

0?  I mean, I might have expected an integer overflow, but 0?

And now the really odd part... take away one zero

Prelude> let d = [1..100000000000] :: [Int]
Prelude> sum d
< ghci crashes and quits >

A slightly more reasonable number..

Prelude> let d = [1..10000000] :: [Int]
Prelude> sum d
*** Exception: stack overflow

At least I can appreciate what is going on in this one  :)

I'm aware that this is a silly way to sum from 1 to n, but I am at a 
loss to explain the above behavior (Except the stack overflow, that is 
fair enough).
I'm also aware that Int is a machine Int, not a clever infinite haskell Int.

Any ideas?

All the best,

Phil


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

Message: 2
Date: Wed, 28 Apr 2010 22:46:11 +0100
From: Philip Scott <[email protected]>
Subject: Re: [Haskell-beginners] [Int] oddness with summing large
        lists
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

Hi Scott,

Thanks for the reply,

> I can't really do too much with it because I don't have any swap on
> this machine, but I definately don't get
> 0 for your first example.
>    
ghci quits rather ungracefully when I do that :)

It is a silly thing to be doing, granted - just wondering if what I saw 
was expected behavior. Perhaps it is the OS (Windows 7) killing the 
process for using to much memory - but I would've thought I would get a 
stack overflow before that happens.

Ho hum!

- Philip

> On Wed, Apr 28, 2010 at 4:32 PM, Philip Scott
> <[email protected]>  wrote:
>    
>> Hi all,
>>
>> I kind of stumbled into this while doing something else - but I thought it
>> was worth posting since I have never actually managed to crash ghci before
>> :)
>>
>> Prelude>  let d = [1..1000000000000] :: [Int]
>> Prelude>  sum d
>> 0
>>
>> 0?  I mean, I might have expected an integer overflow, but 0?
>>
>> And now the really odd part... take away one zero
>>
>> Prelude>  let d = [1..100000000000] :: [Int]
>> Prelude>  sum d
>> <  ghci crashes and quits>
>>
>> A slightly more reasonable number..
>>
>> Prelude>  let d = [1..10000000] :: [Int]
>> Prelude>  sum d
>> *** Exception: stack overflow
>>
>> At least I can appreciate what is going on in this one  :)
>>
>> I'm aware that this is a silly way to sum from 1 to n, but I am at a loss to
>> explain the above behavior (Except the stack overflow, that is fair enough).
>> I'm also aware that Int is a machine Int, not a clever infinite haskell Int.
>>
>> Any ideas?
>>
>> All the best,
>>
>> Phil
>> _______________________________________________
>> Beginners mailing list
>> [email protected]
>> http://www.haskell.org/mailman/listinfo/beginners
>>
>>      
>    



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

Message: 3
Date: Thu, 29 Apr 2010 00:37:18 +0200
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] [Int] oddness with summing large
        lists
To: [email protected], [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain;  charset="iso-8859-1"

Am Mittwoch 28 April 2010 23:32:10 schrieb Philip Scott:
> Hi all,
>
> I kind of stumbled into this while doing something else - but I thought
> it was worth posting since I have never actually managed to crash ghci
> before :)
>
> Prelude> let d = [1..1000000000000] :: [Int]
> Prelude> sum d
> 0
>
> 0?  I mean, I might have expected an integer overflow, but 0?

I can deduce that you have a 32-bit system (as I do).
Because:

Prelude> 1000000000000 :: Int
-727379968

So ([1 .. 1000000000000] :: [Int]) == [] and sum [] == 0 isn't surprising 
at all.

>
> And now the really odd part... take away one zero
>
> Prelude> let d = [1..100000000000] :: [Int]

Prelude> 100000000000 :: Int
1215752192

, which is a pretty large number. Since you gave the list a name, it stays 
in memory. For each Int in the list, at least 3 machine words are needed 
(one for the Int, pointer from cons-cell to value, pointer to next cons-
cell; actually, I think the overhead is larger), so if you have less than 
14GB of RAM, your ghci can't do it.

> Prelude> sum d
> < ghci crashes and quits >
>
> A slightly more reasonable number..
>
> Prelude> let d = [1..10000000] :: [Int]
> Prelude> sum d
> *** Exception: stack overflow
>
> At least I can appreciate what is going on in this one  :)

That works with

Prelude Data.List> foldl' (+) 0 [1 .. 10000000] :: Int
-2004260032
Prelude Data.List> :set +s
Prelude Data.List> foldl' (+) 0 [1 .. 100000000] :: Int
987459712
(3.47 secs, 4820224616 bytes)
Prelude Data.List> foldl' (+) 0 [1 .. 100000000000] :: Int
2103145472
(44.75 secs, 58584335012 bytes)

>
> I'm aware that this is a silly way to sum from 1 to n, but I am at a
> loss to explain the above behavior (Except the stack overflow, that is
> fair enough).
> I'm also aware that Int is a machine Int, not a clever infinite haskell
> Int.
>
> Any ideas?
>
> All the best,
>
> Phil


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

Message: 4
Date: Thu, 29 Apr 2010 09:31:41 +0300
From: Aur Saraf <[email protected]>
Subject: [Haskell-beginners] Feedback on google code jam practice
To: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

Hello,

New to this list, so hello all :-).


I've uploaded practice solutions for google code jam questions here:
http://github.com/SonOfLilit/codejam .

As I'm still learning idiomatic Haskell, I'd love to get feedback on my code.


How would /you/ do it?



Thanks a lot,
   -- Aur


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

Message: 5
Date: Thu, 29 Apr 2010 05:46:14 -0700 (PDT)
From: Hein Hundal <[email protected]>
Subject: Re: [Haskell-beginners] Re: Iterating through a list of
        char...
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=iso-8859-1

Jean-Nicolas,

   Here is a variation on Corentin's solution:

-- This function replaces every character that 
-- follows an 'a' with 'A'

repA :: String -> String

repA s = zipWith f (' ':s) s where
   f 'a' y = 'A'
   f  x  y =  y 

Cheers,
Hein


> From: Jean-Nicolas Jolivet <[email protected]>
>
> I'm trying to iterate through each character of a string
> (that part I
> can do!) however, I need to apply a transformation to each
> character...based on the previous character in the string!
> This is the part I have no clue how to do!
> [snip]
> while i < my_string length:
>     if my_string[i-1] == some_char:
>         do something with
> my_string[i]
>     else
>         do something else
> with my_string[i]



      


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

Message: 6
Date: Thu, 29 Apr 2010 15:52:40 +0200
From: jean verdier <[email protected]>
Subject: Re: [Haskell-beginners] Re: Iterating through a list of
        char...
To: [email protected]
Message-ID: <1272549160.2321.3.ca...@localhost>
Content-Type: text/plain; charset="UTF-8"

I may have missed it in this thread, but if not, why didn't anyone
suggest:

trans []         = []
trans [x]        = [x]
trans ('a':_:xs) = 'a' : 'A' : trans xs 
trans     (x:xs) =        x  : trans xs




On Thu, 2010-04-29 at 05:46 -0700, Hein Hundal wrote:
> Jean-Nicolas,
> 
>    Here is a variation on Corentin's solution:
> 
> -- This function replaces every character that 
> -- follows an 'a' with 'A'
> 
> repA :: String -> String
> 
> repA s = zipWith f (' ':s) s where
>    f 'a' y = 'A'
>    f  x  y =  y 
> 
> Cheers,
> Hein
> 
> 
> > From: Jean-Nicolas Jolivet <[email protected]>
> >
> > I'm trying to iterate through each character of a string
> > (that part I
> > can do!) however, I need to apply a transformation to each
> > character...based on the previous character in the string!
> > This is the part I have no clue how to do!
> > [snip]
> > while i < my_string length:
> >     if my_string[i-1] == some_char:
> >         do something with
> > my_string[i]
> >     else
> >         do something else
> > with my_string[i]
> 
> 
> 
>       
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners




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

Message: 7
Date: Thu, 29 Apr 2010 16:22:33 +0200
From: David Virebayre <[email protected]>
Subject: Re: [Haskell-beginners] Re: Iterating through a list of
        char...
To: jean verdier <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=UTF-8

On Thu, Apr 29, 2010 at 3:52 PM, jean verdier <[email protected]> wrote:
> I may have missed it in this thread, but if not, why didn't anyone
> suggest:
>
> trans []         = []
> trans [x]        = [x]
> trans ('a':_:xs) = 'a' : 'A' : trans xs
> trans     (x:xs) =        x  : trans xs

While as a beginner (I still am !) I would come up with a solution
like this one, on the long run it helps trying to solve those problem
with maps, folds, filters, and zips: Eventually, you'll find the code
more readable, easier to write,  and there's perhaps a better chance
that it can be optimised by ghc.

David.


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

Message: 8
Date: Thu, 29 Apr 2010 10:48:15 -0400
From: matthew coolbeth <[email protected]>
Subject: Re: [Haskell-beginners] Re: Iterating through a list of
        char...
To: David Virebayre <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="utf-8"

I understand that higher-order functions  are incredibly powerful, and that
you can do essentially anything you might ever want while using only 'map'
and 'foldl'.

However,  I have trouble believing that even experienced programmers wold
find such HOF-oriented code to be more readable than Mr Akgun's solution:

foo :: (a -> a -> a) -> [a] -> [a]
foo f (x:y:rest) = f x y : foo f (y:rest)
foo f _ = []

It seems to me that 'foo', as defined here, is a direct restatement of the
original program specification (the english one).  Basically no translation
required.

If anyone here is enough of a veteran to prefer a map/fold implementation of
this spec,  I would be interested to hear about the thought processes you
undertake when writing such code.

Thanks.

On Thu, Apr 29, 2010 at 10:22, David Virebayre
<[email protected]<dav.vire%[email protected]>
> wrote:

> On Thu, Apr 29, 2010 at 3:52 PM, jean verdier <[email protected]>
> wrote:
> > I may have missed it in this thread, but if not, why didn't anyone
> > suggest:
> >
> > trans []         = []
> > trans [x]        = [x]
> > trans ('a':_:xs) = 'a' : 'A' : trans xs
> > trans     (x:xs) =        x  : trans xs
>
> While as a beginner (I still am !) I would come up with a solution
> like this one, on the long run it helps trying to solve those problem
> with maps, folds, filters, and zips: Eventually, you'll find the code
> more readable, easier to write,  and there's perhaps a better chance
> that it can be optimised by ghc.
>
> David.
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>



-- 
mac
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20100429/8f307c8c/attachment.html

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

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


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

Reply via email to