Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Your favourite Haskell Haikus

Name: Anonymous 2015-03-01 18:12

Post your favourite Haskell Haikus! I'll start:

Prelude> foldl (+) 0 [1..100000000]
<interactive>: out of memory

Prelude> foldr (+) 0 [1..100000000]
<interactive>: out of memory

Prelude> id id id id id id id id id id id id id id id id id id id id id id id id id id id id id id id id id id id id id id id id id id 1
<interactive>: out of memory

Name: Anonymous 2015-03-01 18:14

LOL idiot.

Name: Anonymous 2015-03-01 18:35

Shouldn't these take O(cpus * cpus / 2) space at most?

Name: Anonymous 2015-03-01 18:36

id id id idiot

Name: Anonymous 2015-03-01 18:58

lazy by default is so good

Name: Anonymous 2015-03-01 19:37

Just use foldl'

Name: Anonymous 2015-03-01 20:00

>>3
Depends on your definition of "shouldn't". If you're asking whether or not you'd like a language you use for non-toy projects to behave like that, then no, it probably shouldn't. If you're wondering if Haskell shouldn't do that, i.e. if those are bugs, then no, at least the first two are inevitable consequences of its design.

foldr runs out of memory here because each (+) tries to evaluate both of its arguments and causes foldr to get to the end of the list before anything is computed. You can't use foldr with strict functions on big lists.

foldl runs out of memory because it happily builds a list of not-evaluated (+) application as it goes through the entire list, with the same result. You can't use it with big lists period, you should use the strict version, foldl'.

Why foldl is even in the standard library then is a deep philosophical question, I can only humbly repeat this attempt at answering it:

𝓗𝓪𝓼𝓴𝓮𝓵𝓵 𝓻𝓮𝓹𝓻𝓮𝓼𝓮𝓷𝓽𝓼 𝓽𝓱𝓮 𝓬𝓸𝓶𝓹𝓵𝓮𝓽𝓮𝓶𝓸𝓼𝓽 𝓮𝔁𝓪𝓶𝓹𝓵𝓮 𝓸𝓯 𝓾𝓷𝓻𝓮𝓼𝓽𝓻𝓪𝓲𝓷𝓮𝓭
𝓪𝓮𝓼𝓽𝓱𝓮𝓽𝓲𝓬𝓲𝓼𝓶 𝓪𝓷𝓭 𝓯𝓵𝓪𝓶𝓫𝓸𝔂𝓪𝓷𝓽 𝓱𝓸𝓶𝓸𝓼𝓮𝔁𝓾𝓪𝓵𝓲𝓽𝔂 𝓲𝓷 𝓽𝓱𝓮 𝓪𝓻𝓮𝓪 𝓸𝓯 𝓹𝓻𝓸𝓰𝓻𝓪𝓶𝓶𝓲𝓷𝓰
𝓵𝓪𝓷𝓰𝓾𝓪𝓰𝓮 𝓻𝓮𝓼𝓮𝓪𝓻𝓬𝓱, 𝓐𝓹𝓸𝓵𝓵𝓸𝓷𝓲𝓪𝓷 𝓫𝓮𝓪𝓾𝓽𝔂 𝓽𝓪𝓴𝓮𝓷 𝓽𝓸 𝓽𝓱𝓮 𝓪𝓫𝓼𝓸𝓵𝓾𝓽𝓮, 𝓪 𝓹𝓮𝓻𝓯𝓮𝓬𝓽
𝓬𝓸𝓶𝓹𝓸𝓼𝓲𝓽𝓲𝓸𝓷 𝓸𝓯 𝓻𝓮𝓰𝓾𝓵𝓪𝓻, 𝓬𝓵𝓮𝓪𝓷, 𝔀𝓮𝓵𝓵-𝓭𝓮𝓯𝓲𝓷𝓮𝓭 𝓼𝔂𝓷𝓽𝓪𝔁 𝔀𝓲𝓽𝓱 𝓳𝓾𝓼𝓽 𝓮𝓷𝓸𝓾𝓰𝓱
𝓽𝓪𝓼𝓽𝓮𝓯𝓾𝓵𝓵𝔂 𝓲𝓷𝓽𝓻𝓸𝓭𝓾𝓬𝓮𝓭 𝓹𝓮𝓬𝓾𝓵𝓲𝓪𝓻𝓲𝓽𝓲𝓮𝓼 𝓽𝓸 𝓶𝓪𝓴𝓮 𝓲𝓽 𝓮𝔁𝓹𝓻𝓮𝓼𝓼 𝓪𝓷𝓭 𝓭𝓮𝓯𝓲𝓷𝓮
𝓽𝓱𝓮 𝓻𝓮𝓵𝓪𝓽𝓲𝓸𝓷𝓼𝓱𝓲𝓹 𝓫𝓮𝓽𝔀𝓮𝓮𝓷 𝓖𝓸𝓭 𝓪𝓷𝓭 𝓜𝓪𝓷.

𝓘𝓽 𝓲𝓼 𝓪 𝔀𝓸𝓻𝓴 𝓸𝓯 𝓯𝓲𝓷𝓮𝓼𝓽 𝓪𝓻𝓽, 𝓼𝓽𝓪𝓻𝓽𝓮𝓭 𝓫𝔂 𝓭𝓲𝓿𝓲𝓷𝓮 𝓼𝓹𝓪𝓻𝓴 𝓪𝓷𝓭 𝓬𝓪𝓻𝓻𝓲𝓮𝓭 𝓸𝓾𝓽
𝓫𝔂 𝓽𝓮𝓷𝓭𝓮𝓻 𝓪𝓷𝓭 𝓼𝓮𝓷𝓼𝓲𝓽𝓲𝓿𝓮 𝓱𝓪𝓷𝓭𝓼 𝓸𝓯 𝓽𝓱𝓮 𝓼𝓬𝓾𝓵𝓹𝓽𝓸𝓻.

The id thing is more of a lack of a feature, however. It's a generic function, id :: a -> a, when applied to itself (a -> a) gets substituted as both instances of the type parameter, and so on. For some reason ghci doesn't share stuff between these types, so it gets out of hand pretty fast.

Note that ghc actually does that, how they managed to achieve this, isn't it supposed to be the same actual compiler in both instances, is yet another mystery of Haskell.

>>2,4 is pretty butthurt, I'm pleased.

Name: Anonymous 2015-03-01 20:46

>>7
I mean the implementation should recognize (+) as associative and maybe spin up a few threads to do it in parallel. Or don't bother.

The id thing is stupid. OCaml has a problem with it too. But pathological behaviour in HM is nothing new.

Name: Anonymous 2015-03-01 23:04

>>8
I mean the implementation should recognize (+) as associative and maybe spin up a few threads to do it in parallel. Or don't bother.

Dude, I think that for starters not actually dying would be kinda neat.

Name: Anonymous 2015-03-01 23:26

>>9
The key to "not actually dying" in foldr is to take advantage the supplied function's associativity. Then substitute a working foldl or do a reasonable parallelization.

In foldl, the runtime should be smarter about pathological laziness (like it seems to be about memoization.) foldl' shouldn't be needed to prevent oom conditions (though I wouldn't get rid of it.)

Name: Anonymous 2015-03-01 23:38

>>10
And if the function isn't associative, try to generate the list in reverse instead and do foldl with that.

Name: Anonymous 2015-03-02 0:37

hotpants

Name: Anonymous 2015-03-02 1:36

>>11
That might be difficult. If you know you can do it efficiently, fine. But computing fibs in reverse for some huge number isn't going to fix foldr. Or maybe it will, Haskell performance is capricious business.

Name: Anonymous 2015-03-02 2:01

>>13
It would depend if the list offered the capability or not. [1..n] does. fibs does not.

Name: Anonymous 2015-03-03 14:46

Your favourite Haskell Waifus

Don't change these.
Name: Email:
Entire Thread Thread List