[Haskell-cafe] what I learnt from my first serious haskell programm

David House dmhouse at gmail.com
Sat Mar 17 15:23:06 EDT 2007


On 17/03/07, Fawzi Mohamed <fmohamed at mac.com> wrote:
> * namespaces *
>
> First off something that disturbed me but does not seem to be discussed
> much are namespaces, or rather the lack of them.

I'm also in the middle of writing a medium-sized program in Haskell,
but my experiences have been somewhat the opposite; I've found that
although most people complain about the lack of namespaces I haven't
really missed them.

> Yes I know that haskell does have namespaces, but with object oriented
> languages you basically get a namespace for each class, in haskell you
> need modules, and I often like to have groups of related types in the
> same module.

Surely within a group of related types there'd be no overlapping names anyway?

> Records then also put everything in the module namespace, and it seems
> that this misfeature has already been discussed.

I like to prefix my record accessors with three letters that describe
the type. For example, in my forum software, the author of a post can
be pulled out of a Post value using pstAuthor. Although this is
somewhat low-tech, it's a solution that works well and makes reading
code easier, while of course solving the one-namespace problem. I
don't really see why anything more complex would be needed.

> So I am wondering how people cope with them,  share your opinion,
> for me the best thing seem to be to try to use one
> module per "big" type, and then "import qualified x as y", what are
> good coding practices?

A practice I've seen a lot in small- to mid-sized programs is to have
a Types module that contains definitions of the types used in the
program.

> * vector & matrices *
>
> A thing that bothered me a little was the absence of good standardized
> vectors and matrices, for now I rolled my own 3x3, I have seen numerical
> prelude, maybe in the future thre will be a standard interface for matrixes...
> Rolling my own mattrixes/vectors I have hit against some limitations of
> classes, nothing terrible, but not so nice, and something that I gather is
> known for example the fact that you cannot overload + or (in haskell 98)
> you cannot have overlapping instances.

Why not write up your module then send it off to the
libraries at haskell.org mailing list? If this has frustrated you then
it's probably frustrated others. Why not contribute something back and
polish this problem off?

And you can overload (+), just make your matrix type an instance of
Num. If some operations shouldn't be supported (like signum, perhaps),
the general strategy is just to use error (this is basically due to
the fact that Num has too many methods because we don't have a sane
way of splitting up type classes. Type class synonyms -- google for
them -- look like a good solution, but are lacking an implementation).

-- 
-David House, dmhouse at gmail.com


More information about the Haskell-Cafe mailing list