Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Being lazy evaluated, Haskell, makes it hard to reason about performance in more than trivial code (as a rubik cube solver would be).

All those suspended computations may eat your RAM for no good reason at all. For example:

    sum (replicate n 0)
has a space leak O(n). While:

    foldl' (+) 0 (replicate n 0)
works fine O(1) space wise.

My point is that it is easy to eat all your RAM using haskell even in the simple `sum` case above. More so in more involved computations.

Of course, if you can compute your solution in constant space using C, you can also do it in haskell. But writing it in Haskell would require careful thought in how your code is evaluated.

EDIT: It is about trade offs.

If you want declarative and readabe code, with little memory layout noise (allocate memory, assign variables, etc...) then haskell is a reasonable choise. You can write fairly readable clode for complex algorithms. If you are an expert, you can also make it efficient on the first go (speed and space wise). If speed is of upmost importance, then you can write more compiler specific code but with a lot of added noise.

Or you can start with C, write less (than haskell) declarative code and less readable code. It would be harder to get right(the more complex the computation the harder to debug), but when you are done you probably have efficent code. If you have the experience, you may be able to make it fairly readable too.




I just tried this program "sum (replicate n 0)" and it didn't leak at all on n=10000000000

Full source: https://gist.github.com/k-bx/4a7729bcf2386583583b95bed7d4f26...

Also, Haskell has a nice LANGUAGE Strict pragma since recently. Won't save you from things like lazy pairs or other imported things, of course, but at least can be good for one-file scripts where you want to solve Rubic's cube.


But if you try it on ghci you will see that it breaks. (ghc 8.0.1). So it dependes on how you "interpret" the code. `ghc -O` is sophisticated enough to make the right decisions when compiling your code. You probably don't want a space leak, so it doesn't generate one.

GHC will have more trouble making the right choice in more involved computations. It will revert to "just do what the coder says".

Consider this C code for example:

    unsigned int sum = 0;
    const unsigned int n = 1000;
    for(int i = 0; i<n; i++) {
        sum += 0;
    }
If you look at it, you can tell this is O(1) in space for any valid `n`. A spohisticated enough compiler may replace it by

    unsigned int sum = 0;
and be done with it. But even an unsophisticated(but correct) compiler will produce O(1) space wise code.

EDIT: format

EDIT: What I was trying to say in my original comment is that, in haskell, space leaks my appear even in the most innocent looking code. And that it may require a bit of experience to identify and avoid them.


Well, sure, running code in interpreter does less optimizations than doing full compilation. I can write an interpreter for your C code which runs out of memory if you want :) So your "you can tell" depends on particular compiler or interpreter implementation, and no, you can't tell what my compiler will do.


I'll have to agree with that. But then I would also say that I had to make some assumptions, right? :)

I think GHCi is helpful in this situation, however. It clearly distinguishes between leaky and non-leaky code. This leak is a product of "just doing what the coder says" (go to "Data.Monoid.sum" and do what it says there with such and such...). I'm assuming that, for this code, GHCi is bug-free.

I think

    sum (replicate n 0)
is as dangerous as

    foldl (+) 0 (replicate n 0)
or

    foldr (+) 0 (replicate n 0)
. `sum (replicate n 0)` may be the right thing to do in some cases unknown to me.

There is `{-# LANGUAGE Strict #-}`, as you mention, and BangPatterns for strict arguments. But I would assume, those are used only by coders already familiar with haskell and its leaky pitfalls.




Consider applying for YC's Fall 2025 batch! Applications are open till Aug 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: