[Haskell-cafe] missing optimization for (++)
ietf-dane at dukhovni.org
Sun Mar 4 20:23:08 UTC 2018
> On Mar 4, 2018, at 4:32 AM, Ben Franksen <ben.franksen at online.de> wrote:
> I found that in base  we have
> (++)  ys = ys
> (++) (x:xs) ys = x : xs ++ ys
> I had expected there to be a clause
> (++) xs  = xs
> at the top, which would avoid the list traversal in this common case.
> which still traverses xs even if ys=.
> Any thoughts?
Haskell is lazy, the traversal of x happens only when the combined list
is actually traversed, and only for as many elements as requested, so
the construction of the concatenated list carries little additional cost.
And as pointed out, needlessly forcing the first element of y can have
GHCi, version 8.0.2: http://www.haskell.org/ghc/ :? for help
Prelude> let x = [1..]
Prelude> let y = [1..]
Prelude> let z = x ++ y
Prelude> take 20 z
More information about the Haskell-Cafe