[Haskell-cafe] flatten a nested list

Tomasz Zielonka tomasz.zielonka at gmail.com
Fri Dec 29 09:26:49 EST 2006


On Fri, Dec 29, 2006 at 02:06:32PM +0000, Paul Moore wrote:
> Speaking as a relative newbie to Haskell, the thing that tripped me up
> was the fact that you can't have nested lists like the Lisp '(1 (2 (3
> 4) 5)) example in Haskell, because its type is not well-defined.

More precisely: You can't ununiformly nest standard [] lists. By
ununiformly I mean: with leaves on different depths.

You can do it with another list (or rather tree) implementation.

You can nest [] lists uniformly, ie. [[1], [2,3,4]] is a nested list.

> OTOH, it's not entirely clear to me if the issue would come up in
> "real" code.

It depends on what you mean by "issue". If syntactical overhead is an
issue, then it comes up. For me it's a small issue, if at all.

> Slinging about arbitrarily nested lists feels quite natural in Lisp,
> but isn't really idiomatic Haskell.

Nested lists are trees and using tree-like structures in Haskell is
very idiomatic.

Perhaps you would want some syntactic sugar for trees. If [] lists
didn't have sugar in Haskell, they would be as "cumbersome" to use as
trees.

Best regards
Tomasz


More information about the Haskell-Cafe mailing list