5 ms·
Leaf is a data constructor for the type Tree, it's not supposed to be a generally reusable entity, and it wouldn't make sense to treat it as one, just like it w
by calambrac 18y ago
Leaf is a data constructor for the type Tree, it's not supposed to be a generally reusable entity, and it wouldn't make sense to treat it as one, just like it wouldn't make sense to treat a constructor in Java as reusable across classes. There's not really even anything there to reuse - it's just a symbol attached to a very small bit of structure, linked to the data type Tree.
- anamax 18y agoIf trees are made us using Leaf, Node, and Empty, tree "leaks" names, which is a disaster because lots of things have leaves, nodes, and empty. That's what the explanation seems to say. It's unclear if calambrac's comment is meant to say that trees are actually made using something like tree.Leaf(int) or (more likely) tree() by itself and type disambiguation. One reason why I've never looked seriously at Haskell is that all of the examples that I've seen are basically puns and there's no indication as to how one might build larger programs. I'm sure that there's some way to do so, but I assume that the advocates present the language in its best light.
- calambrac 18y agoHaskell has one of the best namespace/module systems of any language I've ever seen. It doesn't 'leak' these names; they're just the names of the data constructors, they're supposed to be visible. If you need to control access, use the module system. I think it's perfectly legitimate for examples of particular features to omit other features (like modules, in this case).
- anamax 18y ago> Haskell has one of the best namespace/module systems of any language I've ever seen. It doesn't 'leak' these names; they're just the names of the data constructors, they're supposed to be visible. If you need to control access, use the module system. If I have to use modules to hide distinguish tree leaves from other leaves, I'm using modules to do what I get from classes in other languages. Since those languages also benefit from modules .... > I think it's perfectly legitimate for examples of particular features to omit other features (like modules, in this case). I agree that any specific example should be targetted. My point is that none of the Haskell examples that I've seen address large programs.
- calambrac 18y agoI mean... you're right, you are using modules to do what you get from classes in other languages. So what? You say this like it's a bad thing, it's not, it's just how this particular problem is solved in Haskell. Different != Horrible. I would argue it's actually a little bit more elegant, in that it takes a problem (namespacing) and solves it one way (modules), rather than having some hybrid package/class system. I'm interested to see the examples you've seen for other languages that do address large programs. I've personally not come across many "How to write Firefox in Python"-style tutorials.
- anamax 18y ago> I mean... you're right, you are using modules to do what you get from classes in other languages. So what? My point is that languages that have classes also seem to need modules. Do haskell's modules address the problems that addressed by the combination of classes and modules in other languages? Does Haskell somehow avoid those problems? Is there some other mechanism? > I'm interested to see the examples you've seen for other languages that do address large programs. Examples of modules can be trivial yet demonstrate why modules might be useful for large programs.
- calambrac 18y agoHaskell isn't object-oriented, so there aren't any little namespaces (classes) running around that themselves need to occupy a namespace (package). So modules are enough. The Haskell wiki entry on modules is short, pretty complete, and has plenty of examples: http://en.wikibooks.org/wiki/Haskell/Modules http://en.wikibooks.org/wiki/Haskell/Modules
- eru 18y ago'If trees are made us using Leaf, Node, and Empty, tree "leaks" names, which is a disaster because lots of things have leaves, nodes, and empty.' Non sequitur? Where is the problem. Try > data MyMaybe a = Just a | Nothing the compiler won't complain about a name crash with the build-in Maybe-Monad. [Sorry, accidentally downmodded the parent-comment from 1 to 0. Slippery fingers.]
- anamax 18y ago>>If trees are made us using Leaf, Node, and Empty, tree "leaks" names, which is a disaster because lots of things have leaves, nodes, and empty.' >Non sequitur? Where is the problem. Try > data MyMaybe a = Just a | Nothing Huh? How does that create a tree instead of a banana? Actually, it's completely unclear what that example is doing. I'd guess that it's specifying a new name and a corresponding value, but I'm not sure which name and what kind of value.
- calambrac 18y agoThe Just and Nothing that he's defining for his MyMaybe are completely different things from the Just and Nothing used for the Maybe that ships in the Prelude. MyMaybe may as well be a banana for all Prelude.Maybe cares.
- eru 18y agoYes. And my code does 'nothing'. It just defines an algebraic datatype.