Some thoughts on writing in Haskell
Recently I've written code for solving diophantine equations of type ax + by + cz + dw = f with genetic algorithm. It's my first "real" program in Haskell with I/O and other stuff. It's a great experience, but I have a question and I would like to read you thoughts about it.
Is it ok to name the same datatype in different ways?
type Chromo = Int
type Gene = (Chromo, Chromo, Chromo, Chromo)
type Diophantine = Gene -> Int
--here it goes
type Population = [Gene]
type Breed = [Gene]
--select n species from Population for interbreeding
select :: Int -> Diophantine -> Population -> Breed
I may write signature for select as
select :: Int -> Diophantine -> Population -> Population
but i think, Breed better express the essence of select. Of course select returns a list of Genes, but these genes are "special". But maybe it's a bad style or make compilers work harder? Maybe someone experienced in Haskell may help.
Also i noticed, that it's convenient to make all I/O in a main function, using non-monadic code in it. I've thought about a process of writing a program in Haskell and i think i know the problem with all Haskell tutorials. Cale Gibbard said that learning Haskell is like learning your firs programming language:
While not really any harder to learn in absolute terms than most of the imperative languages that people use (for instance, it’s far less complicated than C++ or Java), it is sufficiently different from them that you don’t have the same kind of leverage you might be used to when learning something very similar to a language you already know. It’s a lot more like the process of learning your first programming language than moving between, say, C# and Python.
And I agree with him. While I was learning my first programming language, I was also learning about a process of writing a program, I've understood some principles and learned them. In all tutorials about Haskell main themes are the language itself and math, which powers him (category theory, monads, etc.). I think it's necessary to teach the principles of the program writing process too. Like: write the basic functions first, using interactive interpreter (GHCi, pugs) and QuickCheck (any of you favorite test tool) for testing, at the end add I/O, using non-monadic code. Maybe i'm wrong and there is the "right" process, but it is based on my little experience with Haskell :)
P.S. Recently I've discovered and read some chapters from the book Real World Haskell. I think it's great. Thanks to the authors. I want to buy a hardcopy.
P.P.S. Sorry for my english, i will be glad if you help me to fix my mistakes by dropping a note here. :)
3 comments
Jump to comment form