[Haskell-beginners] question >>= [style, efficiency]

John Dorsey haskell at colquitt.org
Wed May 6 11:30:14 EDT 2009


Joel,

> I can think of a variety of ways to put a value at the end of a list
> of the same type.
[...]
> I'd appreciate feedback/advice on whether:
> 1) I've missed any clearly-better alternatives;
> 2) any of those is obviously better or worse than the others;
> 3) I should have known where to look for the answer myself (instead of
> brute-force Googling).

Appending to a normal list is inherently messy and expensive.  If it's
doesn't have to be a normal list, though, things can get better.  Look into
"difference lists" as one example.  Append for cheap while building, then
get a list for linear-time cost when you're finished.

The best data structure depends on what you need it to do.

That said, your "obvious brute force" is the best of the ones you wrote,
specifically because it's "obvious".  It's clear what it does, it reuses
existing functions well, and it's at least roughly as good as the others.

John



More information about the Beginners mailing list