Is there a library that has a strict version of `sum`?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
26 messages Options
12
Reply | Threaded
Open this post in threaded view
|

Is there a library that has a strict version of `sum`?

Damian Nadales
I thought the answer would be easy to find on the internet, but I cannot find any package that features a strict version of `sum`.

Do you know if such a function is defined anywhere? (like a custom prelude for instance)

I know it is trivial to define, but I would prefer not to repeat myself (and somebody else).

Thanks!

_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
Reply | Threaded
Open this post in threaded view
|

Re: Is there a library that has a strict version of `sum`?

Artem Pelenitsyn
Hello Damian,

If anything, I would expect this function to be called sum'. Hayoo knows just a handful of such functions, only one of which does what you want:


This doesn't imply that there is no such function at all: the name could be different or Hayoo might have troubles to find it.

--
Best, Artem

On Tue, 14 Aug 2018, 12:10 Damian Nadales, <[hidden email]> wrote:
I thought the answer would be easy to find on the internet, but I cannot find any package that features a strict version of `sum`.

Do you know if such a function is defined anywhere? (like a custom prelude for instance)

I know it is trivial to define, but I would prefer not to repeat myself (and somebody else).

Thanks!
_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.

_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
Reply | Threaded
Open this post in threaded view
|

Re: Is there a library that has a strict version of `sum`?

Vanessa McHale
In reply to this post by Damian Nadales

If you'd rather import a custom prelude instead of writing `sum = foldl' (+) 0` you might want to think about how dependencies affect maintaining a package.

What's the actual problem you're trying to solve?


On 08/14/2018 04:09 AM, Damian Nadales wrote:
I thought the answer would be easy to find on the internet, but I cannot find any package that features a strict version of `sum`.

Do you know if such a function is defined anywhere? (like a custom prelude for instance)

I know it is trivial to define, but I would prefer not to repeat myself (and somebody else).

Thanks!


_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.

_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.

signature.asc (499 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Is there a library that has a strict version of `sum`?

Imants Cekusins
`sum = foldl' (+) 0`

here is another possibility with "sum" in it:

Prelude Data.Monoid> let f = getSum . mconcat . (Sum <$>)
Prelude Data.Monoid> f [1,2,3,4]
10



On 14 August 2018 at 17:24, Vanessa McHale <[hidden email]> wrote:

If you'd rather import a custom prelude instead of writing `sum = foldl' (+) 0` you might want to think about how dependencies affect maintaining a package.

What's the actual problem you're trying to solve?


On 08/14/2018 04:09 AM, Damian Nadales wrote:
I thought the answer would be easy to find on the internet, but I cannot find any package that features a strict version of `sum`.

Do you know if such a function is defined anywhere? (like a custom prelude for instance)

I know it is trivial to define, but I would prefer not to repeat myself (and somebody else).

Thanks!


_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.

_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.


_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
Reply | Threaded
Open this post in threaded view
|

Re: Is there a library that has a strict version of `sum`?

David Kraeutmann

That isn't strict.

On 8/14/2018 4:38 PM, Imants Cekusins wrote:
`sum = foldl' (+) 0`

here is another possibility with "sum" in it:

Prelude Data.Monoid> let f = getSum . mconcat . (Sum <$>)
Prelude Data.Monoid> f [1,2,3,4]
10



On 14 August 2018 at 17:24, Vanessa McHale <[hidden email]> wrote:

If you'd rather import a custom prelude instead of writing `sum = foldl' (+) 0` you might want to think about how dependencies affect maintaining a package.

What's the actual problem you're trying to solve?


On 08/14/2018 04:09 AM, Damian Nadales wrote:
I thought the answer would be easy to find on the internet, but I cannot find any package that features a strict version of `sum`.

Do you know if such a function is defined anywhere? (like a custom prelude for instance)

I know it is trivial to define, but I would prefer not to repeat myself (and somebody else).

Thanks!


_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.

_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.


_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.

_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
Reply | Threaded
Open this post in threaded view
|

Re: Is there a library that has a strict version of `sum`?

Damian Nadales
In reply to this post by Vanessa McHale
Ok, so if understood the bottom line of the answers I got from Vanessa and Hiromi the take away seems to be: if you're prototyping then stick to the lazy version of sum, otherwise use more advanced data structures (Vectors or Folds).

On Tue, Aug 14, 2018 at 5:18 PM Vanessa McHale <[hidden email]> wrote:

Actually, vector has a sum function already: http://hackage.haskell.org/package/vector-0.12.0.1/docs/Data-Vector.html#v:sum

It's strict, and I'd guess more efficient than foldl' on lists.


On 08/14/2018 09:44 AM, Vanessa McHale wrote:

To be more explicit: unless you've actually benchmarked sum vs sum' this seems to be a premature optimization and you'd be better off just using the stock sum with a sensible data structure.


On 08/14/2018 09:40 AM, Damian Nadales wrote:


On Tue, Aug 14, 2018 at 4:25 PM Vanessa McHale <[hidden email]> wrote:

If you'd rather import a custom prelude instead of writing `sum = foldl' (+) 0` you might want to think about how dependencies affect maintaining a package.

What's the actual problem you're trying to solve?

Just add a list of values. I was expecting such a function to be already defined somewhere else.

Personally, in terms of cognitive complexity I find `sum` less complex than `fold' (+) 0`...
 


On 08/14/2018 04:09 AM, Damian Nadales wrote:
I thought the answer would be easy to find on the internet, but I cannot find any package that features a strict version of `sum`.

Do you know if such a function is defined anywhere? (like a custom prelude for instance)

I know it is trivial to define, but I would prefer not to repeat myself (and somebody else).

Thanks!


_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.

--



Vanessa McHale
Functional Compiler Engineer | Chicago, IL

Website: www.iohk.io
Twitter: @vamchale
PGP Key ID: 4209B7B5

Input
            Output

Twitter Github LinkedIn


This e-mail and any file transmitted with it are confidential and intended solely for the use of the recipient(s) to whom it is addressed. Dissemination, distribution, and/or copying of the transmission by anyone other than the intended recipient(s) is prohibited. If you have received this transmission in error please notify IOHK immediately and delete it from your system. E-mail transmissions cannot be guaranteed to be secure or error free. We do not accept liability for any loss, damage, or error arising from this transmission

--



Vanessa McHale
Functional Compiler Engineer | Chicago, IL

Website: www.iohk.io
Twitter: @vamchale
PGP Key ID: 4209B7B5

Input
          Output

Twitter Github LinkedIn


This e-mail and any file transmitted with it are confidential and intended solely for the use of the recipient(s) to whom it is addressed. Dissemination, distribution, and/or copying of the transmission by anyone other than the intended recipient(s) is prohibited. If you have received this transmission in error please notify IOHK immediately and delete it from your system. E-mail transmissions cannot be guaranteed to be secure or error free. We do not accept liability for any loss, damage, or error arising from this transmission

_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
Reply | Threaded
Open this post in threaded view
|

Re: Is there a library that has a strict version of `sum`?

Tom Ellis-5
On Tue, Aug 14, 2018 at 05:22:00PM +0200, Damian Nadales wrote:
> Ok, so if understood the bottom line of the answers I got from Vanessa and
> Hiromi the take away seems to be: if you're prototyping then stick to the
> lazy version of sum, otherwise use more advanced data structures (Vectors
> or Folds).

I can't say I understand that bottom line.  A strict sum seems like a very
obvious thing to want to feature very prominently in whatever set of
utilities you choose to base your system on.
_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
Reply | Threaded
Open this post in threaded view
|

Re: Is there a library that has a strict version of `sum`?

Damian


On Tue, Aug 14, 2018, 19:26 Tom Ellis <[hidden email]> wrote:
On Tue, Aug 14, 2018 at 05:22:00PM +0200, Damian Nadales wrote:
> Ok, so if understood the bottom line of the answers I got from Vanessa and
> Hiromi the take away seems to be: if you're prototyping then stick to the
> lazy version of sum, otherwise use more advanced data structures (Vectors
> or Folds).

I can't say I understand that bottom line.  A strict sum seems like a very
obvious thing to want to feature very prominently in whatever set of
utilities you choose to base your system on.
Any idea why Prelude does not ship a sum' version? (i.e. a strict version of sum) 

_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.

_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
Reply | Threaded
Open this post in threaded view
|

Re: Is there a library that has a strict version of `sum`?

Brandon Allbery
I suspect the only reason it includes sum at all is that the Report says it must. Otherwise, well, it's a trivial fold and you can easily select the strictness you want by how you phrase the fold.

On Tue, Aug 14, 2018 at 1:35 PM Damian <[hidden email]> wrote:


On Tue, Aug 14, 2018, 19:26 Tom Ellis <[hidden email]> wrote:
On Tue, Aug 14, 2018 at 05:22:00PM +0200, Damian Nadales wrote:
> Ok, so if understood the bottom line of the answers I got from Vanessa and
> Hiromi the take away seems to be: if you're prototyping then stick to the
> lazy version of sum, otherwise use more advanced data structures (Vectors
> or Folds).

I can't say I understand that bottom line.  A strict sum seems like a very
obvious thing to want to feature very prominently in whatever set of
utilities you choose to base your system on.
Any idea why Prelude does not ship a sum' version? (i.e. a strict version of sum) 

_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.


--
brandon s allbery kf8nh

_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
Reply | Threaded
Open this post in threaded view
|

Re: Is there a library that has a strict version of `sum`?

Bryan Richter-2
In reply to this post by Damian Nadales
On 08/14/2018 11:22 AM, Damian Nadales wrote:

> Ok, so if understood the bottom line of the answers I got from
> Vanessa and Hiromi the take away seems to be: if you're prototyping
> then stick to the lazy version of sum, otherwise use more advanced
> data structures (Vectors or Folds).

Are you implying that lazy functions are only good for prototyping? Or
is there something wrong with sum in particular?

More generally, why isn't sum working for you?


_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.

signature.asc (849 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Is there a library that has a strict version of `sum`?

Vanessa McHale

Yes, that was what I was never quite clear on. What is your *actual problem, in context*, not the problem you think you have because you read elsewhere that foldl' is more efficient?


On 08/14/2018 01:07 PM, Bryan Richter wrote:
On 08/14/2018 11:22 AM, Damian Nadales wrote:

Ok, so if understood the bottom line of the answers I got from
Vanessa and Hiromi the take away seems to be: if you're prototyping
then stick to the lazy version of sum, otherwise use more advanced
data structures (Vectors or Folds).
Are you implying that lazy functions are only good for prototyping? Or
is there something wrong with sum in particular?

More generally, why isn't sum working for you?



_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.

_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.

signature.asc (499 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Is there a library that has a strict version of `sum`?

Francesco Ariis
On Tue, Aug 14, 2018 at 01:09:36PM -0500, Vanessa McHale wrote:
> On 08/14/2018 01:07 pm, bryan richter wrote:
> > Are you implying that lazy functions are only good for prototyping? Or
> > is there something wrong with sum in particular?
> >
> > More generally, why isn't sum working for you?
> yes, that was what i was never quite clear on. what is your *actual
> problem, in context*, not the problem you think you have because you
> read elsewhere that foldl' is more efficient?

OP might have been a bit speculative but come on, let's not make
Damian must feel under examination! :)
_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
Reply | Threaded
Open this post in threaded view
|

Re: Is there a library that has a strict version of `sum`?

Damian Nadales
In reply to this post by Bryan Richter-2


On Tue, Aug 14, 2018 at 8:08 PM Bryan Richter <[hidden email]> wrote:
On 08/14/2018 11:22 AM, Damian Nadales wrote:

> Ok, so if understood the bottom line of the answers I got from
> Vanessa and Hiromi the take away seems to be: if you're prototyping
> then stick to the lazy version of sum, otherwise use more advanced
> data structures (Vectors or Folds).

Are you implying that lazy functions are only good for prototyping? Or
No. I know lazy functions can be quite useful.
 
is there something wrong with sum in particular?

From the point of view of somebody that is not that bright, like myself, it seems. Why would you need to build a huge thunk containing the representation of a sum of values. It is not that you can pattern match on integers or consume the sum incrementally.

I apologize if I infuriate people, but I really don't get the usefulness of a lazy sum function.

 
More generally, why isn't sum working for you?

Because if I want to test my function with 100000000 values it consumes a lot of memory.
 
_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.

_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
Reply | Threaded
Open this post in threaded view
|

Re: Is there a library that has a strict version of `sum`?

Damian Nadales
In reply to this post by Vanessa McHale


On Tue, Aug 14, 2018 at 8:10 PM Vanessa McHale <[hidden email]> wrote:

Yes, that was what I was never quite clear on. What is your *actual problem, in context*, not the problem you think you have because you read elsewhere that foldl' is more efficient?


The problem I have is that sum consumes huge amount of memory. In particular I'm modeling the Chimeric ledgers described in this paper https://eprint.iacr.org/2018/262.pdf. In several places I have to sum up values in different lists, and as soon as I try this with large lists the memory usage blows up. So that's my concrete scenario.

In view of this, I though, "I can write this replacing `sum` by `foldl' (+) 0` but wouldn't be nice if this function was defined already somewhere else?" That's it.

Please ignore this if it is a silly question, and my apologies in advance.
 
On 08/14/2018 01:07 PM, Bryan Richter wrote:
On 08/14/2018 11:22 AM, Damian Nadales wrote:

Ok, so if understood the bottom line of the answers I got from
Vanessa and Hiromi the take away seems to be: if you're prototyping
then stick to the lazy version of sum, otherwise use more advanced
data structures (Vectors or Folds).
Are you implying that lazy functions are only good for prototyping? Or
is there something wrong with sum in particular?

More generally, why isn't sum working for you?



_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.

_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
Reply | Threaded
Open this post in threaded view
|

Re: Is there a library that has a strict version of `sum`?

Imants Cekusins
 I have to sum up values in different lists, and as soon as I try this with large lists the memory usage blows up.

did you try splitting the list in chunks and summing those chunks?

there is this chunksOf function:


_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
Reply | Threaded
Open this post in threaded view
|

Re: Is there a library that has a strict version of `sum`?

Matt
`foldr (+) 0` is going to create a bunch of thunks replacing the spine of the list. This is desirable if the number type can take advantage of laziness in some way (eg a `data Nat = Z | Succ Nat`), but for most cases, the `foldl' (+) 0` implementation is preferable.

`sum' = foldl' (+) 0` would (in my opinion) be a reasonable addition to the Prelude, or at least Data.Foldable, but no one has done the work to do that -- if you raise an issue on the GHC Trac then it will be up for discussion and possible inclusion.

Matt Parsons

On Tue, Aug 14, 2018 at 1:17 PM, Imants Cekusins <[hidden email]> wrote:
 I have to sum up values in different lists, and as soon as I try this with large lists the memory usage blows up.

did you try splitting the list in chunks and summing those chunks?

there is this chunksOf function:


_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.


_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
Reply | Threaded
Open this post in threaded view
|

Re: Is there a library that has a strict version of `sum`?

Brandon Allbery
In reply to this post by Damian Nadales
On Tue, Aug 14, 2018 at 2:57 PM Damian Nadales <[hidden email]> wrote:
In view of this, I though, "I can write this replacing `sum` by `foldl' (+) 0` but wouldn't be nice if this function was defined already somewhere else?" That's it.

There's a certain tendency for people to look for even trivial canned solutions, which to my mind misses the point of functional programming. It's trivial to build exactly what you need from the building blocks you have, so canned solutions like Prelude "sum" lead both to this kind of problem when they don't quite fit, and at the same time inappropriately encourage people to go looking for "more appropriate" canned solutions.
 
--
brandon s allbery kf8nh

_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
Reply | Threaded
Open this post in threaded view
|

Re: Is there a library that has a strict version of `sum`?

Vanessa McHale

Yes, writing sum' = foldl' (+) 0 in a project-specific prelude (or even a local binding) is much more maintainable than grabbing a dependency on a custom prelude.


On 08/14/2018 02:25 PM, Brandon Allbery wrote:
On Tue, Aug 14, 2018 at 2:57 PM Damian Nadales <[hidden email]> wrote:
In view of this, I though, "I can write this replacing `sum` by `foldl' (+) 0` but wouldn't be nice if this function was defined already somewhere else?" That's it.

There's a certain tendency for people to look for even trivial canned solutions, which to my mind misses the point of functional programming. It's trivial to build exactly what you need from the building blocks you have, so canned solutions like Prelude "sum" lead both to this kind of problem when they don't quite fit, and at the same time inappropriately encourage people to go looking for "more appropriate" canned solutions.
 
--
brandon s allbery kf8nh

_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.

signature.asc (499 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Is there a library that has a strict version of `sum`?

Bryan Richter-2
In reply to this post by Damian Nadales
On 08/14/2018 02:57 PM, Damian Nadales wrote:

>
>
> On Tue, Aug 14, 2018 at 8:10 PM Vanessa McHale <[hidden email] <mailto:[hidden email]>> wrote:
>
>     Yes, that was what I was never quite clear on. What is your *actual problem, in context*, not the problem you think you have because you read elsewhere that foldl' is more efficient?
>
>
> The problem I have is that sum consumes huge amount of memory. In particular I'm modeling the Chimeric ledgers described in this paper https://eprint.iacr.org/2018/262.pdf. In several places I have to
> sum up values in different lists, and as soon as I try this with large lists the memory usage blows up. So that's my concrete scenario.
>
> In view of this, I though, "I can write this replacing `sum` by `foldl' (+) 0` but wouldn't be nice if this function was defined already somewhere else?" That's it.
>
> Please ignore this if it is a silly question, and my apologies in advance.
>  
I think your question is not silly, and quite reasonable. I think that giving more context up front, however, would have helped people understand what your were actually asking. Based solely on what
you wrote, you might have been someone who was summing [1..10] for the first time ever and looking for that hypothetical foldl/foldl' analogue, like Vanessa said. :) Being specific helps.

Having said all that, I am also surprised that `sum [1..100000000]` blows up! (But now I see others are responding with rationale for that.)


_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.

signature.asc (849 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Is there a library that has a strict version of `sum`?

Artem Pelenitsyn
Damian,

thank you for this question, The discussion was thought-provoking for me. Going back to your original inquiry, by the way, I've just noticed this prelude where `sum` and `product` are strict: https://github.com/serokell/universum

Bryan,

I still fail to see (in this thread also) any rationale for lazy `sum`. I looked at this2014 Reddit thread, and it seems that not only I am puzzled: https://www.reddit.com/r/haskell/comments/2agxcb/why_is_sum_lazy/

There is also an opinion (from quite authoritative source, may I add)  that `foldl` should be strict, not just `sum`: http://www.well-typed.com/blog/90/

I wonder what does libraries committee think about all that. One way to find… https://mail.haskell.org/cgi-bin/mailman/listinfo/libraries 

--
Best, Artem

On Tue, 14 Aug 2018 at 22:37 Bryan Richter <[hidden email]> wrote:
On 08/14/2018 02:57 PM, Damian Nadales wrote:
>
>
> On Tue, Aug 14, 2018 at 8:10 PM Vanessa McHale <[hidden email] <mailto:[hidden email]>> wrote:
>
>     Yes, that was what I was never quite clear on. What is your *actual problem, in context*, not the problem you think you have because you read elsewhere that foldl' is more efficient?
>
>
> The problem I have is that sum consumes huge amount of memory. In particular I'm modeling the Chimeric ledgers described in this paper https://eprint.iacr.org/2018/262.pdf. In several places I have to
> sum up values in different lists, and as soon as I try this with large lists the memory usage blows up. So that's my concrete scenario.
>
> In view of this, I though, "I can write this replacing `sum` by `foldl' (+) 0` but wouldn't be nice if this function was defined already somewhere else?" That's it.
>
> Please ignore this if it is a silly question, and my apologies in advance.
>  

I think your question is not silly, and quite reasonable. I think that giving more context up front, however, would have helped people understand what your were actually asking. Based solely on what
you wrote, you might have been someone who was summing [1..10] for the first time ever and looking for that hypothetical foldl/foldl' analogue, like Vanessa said. :) Being specific helps.

Having said all that, I am also surprised that `sum [1..100000000]` blows up! (But now I see others are responding with rationale for that.)

_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.

_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
12