News for hackers. Stuff that matters.
Re:What it's like to use Haskell by copergi on Wednesday March 26, 2014 @11:02AM (#7473843)
>Haskell's laziness makes slow code and code that use up a lot of memory hard to locate, and at times require significant refactoring, to correct

This is largely mythology built up around a misunderstanding of the grain of truth at the core of it. Lazyness can create pathological performance issues, just as strictness can. 90% of the time neither matters, 5% of the time you need strictness, 5% of the time you need laziness. Using strictness in haskell is very simple, and knowing when you need to is very easy to learn. The profiling tools make it incredibly obvious when laziness is causing a problem. And I can't even imagine a case where it requires significant refactoring. It is generally adding a couple of "!"s or importing Module.Strict instead of Module.Lazy.

Re:What it's like to use Haskell by chriswarbo on Wednesday March 26, 2014 @11:45AM (#7474191)
> This is largely mythology built up around a misunderstanding of the grain of truth at the core of it.

I agree. I've encountered a few space leaks in my Haskell code, but it's always been pretty easy to track down: profile the memory usage and look for a conspicuously huge peak. It will usually be fixable by forcing an intermediate result, or using a different recursion strategy (eg. a fold instead of explicit recursion). Haskell functions tend to be so small and single-purpose that there are no knock-on effects of doing this refactoring.

I think there's a perception that 'Haskell is difficult to debug', whereas the reality is more like 'Haskell will reject all of the easy bugs'. In other words, the average difficulty of a Haskell bug may be higher, but the density is less.

Re:What it's like to use Haskell by ufo on Wednesday March 26, 2014 @12:21PM (#7474454)
> eg. a fold instead of explicit recursion

Wouldn't adding some bangs in the patterns kind of do the same thing as using foldl'?

Re:What it's like to use Haskell by AaronFriel on Wednesday March 26, 2014 @02:16PM (#7475223)
Yes. Most recommendations for implementing recursion in Haskell that I've seen have amounted to writing it in terms of folds. You gain the benefit of experts vetting the code, and novices being able to understand what it is doing it a glance.
Re:What it's like to use Haskell by tel on Wednesday March 26, 2014 @11:41AM (#7474143)
I'd like to add that for the 90% of the time it doesn't matter, 95% of that time you could use a total language. This is one of the joys of using a total language: seeing what's still easy!
Re:What it's like to use Haskell by theseoafs on Wednesday March 26, 2014 @01:48PM (#7475026)
I see no reason why strictness would have pathological performance problems. If you don't need to compute a value, dinner compute it. No performance problems.

I also see no reason you would need laziness for 5% of programming problems. That seems absurd given that the overwhelming majority of programmers have gotten along without it for 50 years.

Re:What it's like to use Haskell by jroesch on Wednesday March 26, 2014 @04:29PM (#7476386)
Most programmers use laziness all over the place, they unfortunately have few tools for introducing it, and thus don't recognize it as such.

For example in C/C++ your only ability to introduce laziness is via manual thunking, if, or the logical conjunctive (&&), and disjunction (||) which have non-strict semantics. For example you often see the idiom of:

    if (x != NULL && x.foo == bar) { ... }
Which relies on (&&) being lazy in its second argument. Lazy languages allow one to write things like && and if without needing them baked in. For example:

    until :: Bool -> a -> a -> a
    until b t f = case (not b)
      True -> t
      False -> f
Re:What it's like to use Haskell by copergi on Wednesday March 26, 2014 @01:59PM (#7475107)
>I see no reason why strictness would have pathological performance problems. If you don't need to compute a value, dinner compute it. No performance problems.

You just answered your own question. "If you don't need to compute a value, then don't compute it" is laziness.

>That seems absurd given that the overwhelming majority of programmers have gotten along without it for 50 years.

No they haven't. Most programmers don't understand it, but they tend to use it on a regular basis.

Re:What it's like to use Haskell by theseoafs on Wednesday March 26, 2014 @02:09PM (#7475176)
> You just answered your own question. "If you don't need to compute a value, then don't compute it" is laziness.

No -- "laziness" is saying "the programmer wants us to compute this value, but we don't need it just yet, so we're going to create a thunk that will sit on the heap until it's evaluated or garbage collected". Strictness is doing what the programmer says, which involves the programmer deciding what does and does not have to be computed while he's writing the program.

Re:What it's like to use Haskell by copergi on Wednesday March 26, 2014 @02:31PM (#7475344)
Perhaps I was not clear. You stated the very reason for laziness: to not compute things you don't need computed. There are many cases where it is very beneficial to be able to determine this at evaluation time, rather than when writing the code. Hence laziness, and hence it being used all the time in most mainstream languages.
Re:What it's like to use Haskell by theseoafs on Wednesday March 26, 2014 @03:02PM (#7475631)
You say it's used all the time -- so how about you give an example of a "lazy" solution to a problem in, say, C?
Re:What it's like to use Haskell by ssprang on Wednesday March 26, 2014 @03:55PM (#7476102)
I implement laziness "manually" all the time in Objective-C.

For example, imagine something happens which invalidates a property which must be calculated (say, a bounding box for a graphical object).

Instead of immediately recomputing it, just set it to nil, and only recompute it the next time the property is actually accessed.

This way you can harmlessly invalidate it multiple times without doing a potentially expensive calculation (which just gets thrown out by subsequent invalidations).

Re:What it's like to use Haskell by theseoafs on Wednesday March 26, 2014 @05:31PM (#7476813)
That's not laziness, that's just cache invalidation.
Re:What it's like to use Haskell by swift on Wednesday March 26, 2014 @07:44PM (#7477525)
It's fair to say that it's laziness, in my view. That sort of thing is just a manual implementation of what laziness would do in a particular situation. The closure that the thunk would hold is essentially diffused into the object that contains the lazy property.
Re:What it's like to use Haskell by cscheid on Wednesday March 26, 2014 @09:50PM (#7478059)
A fast sorting algorithm in Haskell degrades gracefully to a (faster) selection algorithm when you don't ask for the whole thing.

In other words, "sort lst" gives you a sorted list, but "take 5 (sort lst)" gives you the first five elements, without sorting the rest of the list.

Re:What it's like to use Haskell by mavelikara on Wednesday March 26, 2014 @07:04PM (#7477311)
Java example:

    if (logger.isDebugEnabled())
        logger.debug("..." + ...);
Re:What it's like to use Haskell by geon on Thursday March 27, 2014 @03:56AM (#7479118)
SomeObjec *getObject(){ if (!theObject) theObject = createObject();

return theObject; }

Re:What it's like to use Haskell by copergi on Wednesday March 26, 2014 @03:48PM (#7476025)
foo || bar