[Haskell-cafe] GetOpt

Bulat Ziganshin bulat.ziganshin at gmail.com
Thu Apr 27 07:10:32 EDT 2006


Hello Anton,

Wednesday, April 26, 2006, 11:29:16 PM, you wrote:

> I just started to study Haskell and it is my almost first big experience
> with functional languages (except Emacs Lisp and Python). I enjoyed all
> small exercises and started a bigger business writing a general utility.

you are my ideal client because we both speak Russian (but not here :) )
and both interested in large "real-world" applications :)  download
the http://freearc.narod.ru/FreeArc-sources.tar.gz and enjoy - it's a
full of Russian comments and it's a real-world program that solves
many problems that you yet foresee :)

in particular, i also started with vision of my program (it's a
RAR-like archiver) as a sequence of transformations:

1) first, a command line translated into the program "job" - it's
actually business of GetOpt and not very differ from other language's
implementations

2) second, this job plus information about files on disk is translated
into the record of structure of archive being created

3) third, archive structure translated into the sequence of I/O
operations

but when i started to do the actual implementation, i realized that
such pure functional approach is nor appropriate and at the last end
i wrote the straight imperative program, just using the power of
Haskell language. moreover, in this process i added to the language
many imperative constructs that make imperative programming easier

> However, I have a problem from the beginning. The utility get some file
> and convert it to another format. It is a kind of small compiler. It 
> also accepts many parameters and behaves depending on them. The problem
> is how to do this neat! How should I write my program to accept and 
> neatly work with options????

you can see my solution in Cmdline module - it's one of largest module
in my program. i don't like the GetOpt interface (it returns a list of
options what is unusable for high-speed application) so i implemented
my own option-processing routines, it's just about 50 lines long
(great demonstration of Haskell power!). all processed options are
record in one large record that is passed around all the program. if
you get accustomed to global variables, it's using in Haskell is
possible but that is not the best way. you can also use implicit
parameters (at least in hugs and ghc), but this again makes data
dependencies somewhat non-understandable

btw, i suggest you to use WinHugs for debugging program and ghc for
final compilation. this makes faster development time together with
faster final executable. moreover, making your program compatible with
both environments is almost ensure that it will be compatible with
coming Haskell standard, Haskell-prime

returning back to options parsing - there is an interesting
alternative to GetOpt (which is just mimics corresponding C module) -
it's a PescoCmd:

http://scannedinavian.org/~pesco/distfiles/pesco-cmdline-2.0.tgz
http://scannedinavian.org/~pesco/distfiles/pesco-cmdline-2.0.pdf
http://scannedinavian.org/~pesco/distfiles/pesco-cmdline-man-2.0.pdf

i also recommend you to read several other real-world Haskell program
where you can steal more code and ideas:

http://postmaster.cryp.to/postmaster-2005-02-14.tar.gz
ftp://ftp.cse.unsw.edu.au/pub/users/dons/yi/yi-0.1.0.tar.gz
darcs (darcs get --partial http://www.abridgegame.org/repos/darcs/)
happs (darcs get --partial http://happs.org/HAppS)

and one more interesting source of real-world approach to Haskell
programming:

http://www.haskell.org/haskellwiki/Hitchhikers_Guide_to_the_Haskell


-- 
Best regards,
 Bulat                            mailto:Bulat.Ziganshin at gmail.com



More information about the Haskell-Cafe mailing list